-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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: Propagate URL changes to react state #5188
Conversation
Signed-off-by: Simon Behar <simbeh7@gmail.com>
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.
does this only affect query params?
Correct, prop changes cause a re-render of the whole component and there is no way to change a prop or other variable outside of React's state as there is with query params |
Do we want a utility function for this? It could live with useEffect(historyThing(history, () => {
setShowFlow(p.get('showFlow') === 'true');
setShowWorkflows(p.get('showWorkflows') !== 'false');
setExpanded(p.get('expanded') === 'true');
setSelectedNode(p.get('selectedNode'));
setTab(p.get('tab'));
), [history]); Not sure. Any way to simplify the code, maybe event just use shorter variable names? useEffect(() => {
return history.listen(l => {
const p = new URLSearchParams(l.search);
setSidePanel(p.get('sidePanel') === 'true');
setTab(p.get('tab'));
});
}, [history]); |
Sure, I'll simplify |
I don't feel very strongly about this. I like how |
Signed-off-by: Simon Behar <simbeh7@gmail.com>
Fixes #5186
The react
useState
takes as parameter only an initial value. Once the hooks are created, if the value passed in touseState
changes it has no effect on the state of the hooks.https://reactjs.org/docs/hooks-reference.html#usestate
Since the only way the react state reads the query parameters is as an argument to
useState
, if we change the URL without changing the react state, the change will not propagate to down to the react state. With this change, we use an effect on the history, so if the history were to change, we would make sure to update our react state with new query param values.