-
Notifications
You must be signed in to change notification settings - Fork 386
fix(hooks): compute initial search parameters from widget #3399
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 51b0820:
|
✅ Deploy Preview for react-instantsearch ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
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'll approve this, because I'm not seeing how what I described can cause real issues.
Maybe we should call widget.dispose
after the initial getWidgetRenderState and before it "really gets mounted"? Or some other way to set helper.state back to the previous value after fake rendering (cache state; getWidgetRenderState; reset state;
)?
helper.state = | ||
widget.getWidgetSearchParameters?.(helper.state, { uiState }) || | ||
helper.state; |
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.
this means that if this part of the function gets called, but the widget never actually mounts, there will be stray search parameters not associated to a widget (as we don't call dispose). Could this ever be a real issue?
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.
Why wouldn't the widget mount at this point?
And getWidgetSearchParameters()
returns a value without triggering side effects, so I'm not sure to understand what you're saying.
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.
getWidgetSearchParameters doesn't have any side-effects, but helper.state = getWidgetSearchParameters()
is a side-effect.
I'm not sure why it wouldn't be mounted, but I'm not 100% sure whether the contract of react allows a memo to be called before/after the component is already mounted? Are there any specs of that? Is it possible a memo is called without the rest of the component being called?
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.
Assigning helper.state
is indeed a side effect. I think it's localized in that case because each widget overrides it for its initial rendering, and then the InstantSearch.js lifecycle takes over and sets it.
React can call memo anytime that it wants to call it (for memory/optimization purposes), so there's no certainty that it'll be called only once. I don't think that the new code introduced competes with the existing logic of useConnector()
and useMemo()
though. We already had this consideration before this change.
Description
This changes how we compute the initial Helper state that we give to
widget.getWidgetRenderState()
to retrieve the initial widget React state.The problem originally surfaced when server-side rendering the
<Menu>
component:This happens because the
helper.state
andstate
used to callwidget.getWidgetRenderState()
mismatched. In algolia/instantsearch#4960, we tried to fix this problem by always relying onstate
. But this constraint is hard to respect, and hard to implement in practice because of stale closures (inrefine()
andcreateURL()
).I explain in that PR why we had to do it this way, essentially because the helper state was the resolved helper state, including all the inherited search parameters from the parent indices. With a resolved helper state, InstantSearch considers the parameters as controlled and therefore doesn't get notified of new changes coming from other indices. The part that we were missing is that there's a way to compute the local index search parameters without inheriting the parent index via
index.getWidgetSearchParameters()
.Solution
This PR is another cleaner approach directly in React InstantSearch Hooks. We assign the widget search parameters gotten from
widget.getWidgetSearchParameters()
to the helper state so that the widget is directly aware of its search parameters.Result
<Menu>
throwing about the undeclared facet.useClearRefinements()
anduseCurrentRefinements()
that used to initially render with an empty state. They're now directly aware of their refinements during first render.state
ingetWidgetRenderState
instantsearch#4960