-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Re-running ui code initially rendering the old document #1017
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,14 @@ function WidgetHandler({ | |
initialData: initialDataProp, | ||
}: WidgetHandlerProps): JSX.Element | null { | ||
const { widget, error: widgetError } = useWidget(widgetDescriptor); | ||
const [isLoading, setIsLoading] = useState(true); | ||
const [prevWidget, setPrevWidget] = useState(widget); | ||
// Cannot use usePrevious to change setIsLoading | ||
// Since usePrevious runs in an effect, the value never gets updated if setIsLoading is called during render | ||
if (widget !== prevWidget) { | ||
setPrevWidget(widget); | ||
setIsLoading(true); | ||
} | ||
|
||
const [document, setDocument] = useState<ReactNode>(); | ||
|
||
|
@@ -226,6 +234,7 @@ function WidgetHandler({ | |
const newDocument = parseDocument(documentParam); | ||
setInternalError(undefined); | ||
setDocument(newDocument); | ||
setIsLoading(false); // Must go after setDocument since setters are not batched in effects | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Being pedantic - this comment isn't true. In a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh ya, this is actually a callback that causes it, not the effect. So I don't know if this would be addressed by React 18 That's interesting and I hadn't seen that API before. Would probably be useful here since there are probably 3 render passes here instead of 1. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I updated to use |
||
if (stateParam != null) { | ||
try { | ||
const newState = JSON.parse(stateParam); | ||
|
@@ -339,11 +348,14 @@ function WidgetHandler({ | |
if (error != null) { | ||
return { status: 'error', descriptor: widgetDescriptor, error }; | ||
} | ||
if (isLoading) { | ||
return { status: 'loading', descriptor: widgetDescriptor }; | ||
} | ||
if (renderedDocument != null) { | ||
return { status: 'ready', descriptor: widgetDescriptor }; | ||
} | ||
return { status: 'loading', descriptor: widgetDescriptor }; | ||
}, [error, renderedDocument, widgetDescriptor]); | ||
}, [error, renderedDocument, widgetDescriptor, isLoading]); | ||
|
||
return useMemo( | ||
() => | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we just call this in
initializeWidget
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe. Not sure how if it being in an effect would cause issues. It will cause an unnecessary render cycle probably
https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed this to use
widgetDescriptor
instead.useWidget
async gets the widget from the descriptor, so there's potential latency there (I saw some flashing of the old table before the loader).Putting this in a
useEffect
means we get an extra render of the old table. I put some logging in and with this in anything that relies onwidget
instead ofwidgetDescriptor
, the table unmounts, remounts (old), quickly unmounts (< 100ms), shows loader, then mounts new