Skip to content
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

Merged
merged 3 commits into from Feb 24, 2021

Conversation

simster7
Copy link
Member

@simster7 simster7 commented Feb 24, 2021

Fixes #5186

The react useState takes as parameter only an initial value. Once the hooks are created, if the value passed in to useState changes it has no effect on the state of the hooks.

During the initial render, the returned state (state) is the same as the value passed as the first argument (initialState).

The setState function is used to update the state. It accepts a new state value and enqueues a re-render of the component.

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.

Signed-off-by: Simon Behar <simbeh7@gmail.com>
@simster7 simster7 marked this pull request as draft February 24, 2021 15:55
Signed-off-by: Simon Behar <simbeh7@gmail.com>
@simster7 simster7 marked this pull request as ready for review February 24, 2021 16:20
Copy link
Contributor

@alexec alexec left a 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?

@simster7
Copy link
Member Author

simster7 commented Feb 24, 2021

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

@simster7 simster7 changed the title fix: Propogate URL changes to react state fix: Propagate URL changes to react state Feb 24, 2021
@alexec
Copy link
Contributor

alexec commented Feb 24, 2021

Do we want a utility function for this? It could live with historyUrl and maybe leverage some cleverness. E.g.

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]);

@simster7
Copy link
Member Author

Sure, I'll simplify

@alexec
Copy link
Contributor

alexec commented Feb 24, 2021

I don't feel very strongly about this. I like how useEffect makes it super easy to identify and fix this issue.

Signed-off-by: Simon Behar <simbeh7@gmail.com>
@simster7 simster7 merged commit f5dcd1b into argoproj:master Feb 24, 2021
@simster7 simster7 mentioned this pull request Mar 1, 2021
32 tasks
simster7 added a commit that referenced this pull request Mar 2, 2021
Signed-off-by: Simon Behar <simbeh7@gmail.com>
@simster7 simster7 mentioned this pull request Mar 8, 2021
@simster7 simster7 mentioned this pull request Apr 19, 2021
50 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UI: Back / History Behavior is broken in Workflow Details View
2 participants