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

Sync Problem between "not immediately opened tabs" #2

Open
reynnan opened this issue Jul 16, 2020 · 2 comments
Open

Sync Problem between "not immediately opened tabs" #2

reynnan opened this issue Jul 16, 2020 · 2 comments

Comments

@reynnan
Copy link

reynnan commented Jul 16, 2020

If you open two tabs and one of this tab changes the state before the other one be opened, the second tab it will still initialize with the initValue that doesn't correspond anymore to the real value and if the second tabs change the state it will override the changes made by the first one.

Example:
Using the increment example in the README:

  1. Open the first tab and click to increment + 1
  2. Now open a second tab, it'll show the count as 0 and if you click increment it will "ignore" the first tab change and count as 1 again.

This is not very good when you state depends on the previous state

Possible Solution:
Maybe also save the state value on to use localStorage/sessionStorage and do?:

let initValue = checkOnLocalStorage() || initalState;
let [state, dispatch] = useReducer(reducer, initValue);
@trevoro
Copy link
Owner

trevoro commented Jul 29, 2020

That's a great point and I should highlight it more formally. It's not a distributed state machine, it merely records state transformations across tabs simultaneously. IOW it doesn't persist any state. When I used this in a personal project I used the initial state setting from a global context to get that. My particular use case also was only to specifically deal with tabs that were already open.

If you think there's a clean way to handle it without forcing any localStorage usage or creating reducers, etc I'm all ears.

@reynnan
Copy link
Author

reynnan commented Jul 29, 2020

That's a great point and I should highlight it more formally. It's not a distributed state machine, it merely records state transformations across tabs simultaneously. IOW it doesn't persist any state. When I used this in a personal project I used the initial state setting from a global context to get that. My particular use case also was only to specifically deal with tabs that were already open.

If you think there's a clean way to handle it without forcing any localStorage usage or creating reducers, etc I'm all ears.

@trevoro
Hmm, maybe if we added a new action that will be triggered when "componentDidMount":

React.useEffect(initBroadcast , []);

This action would basically post a message, saying a broadcast was initialized, and if:

  • There is someone listening to this message, broadcast.onmessage send your current state value, and that will be my initial value

  • Noone is listening, my initial value is the one that has been passed through the hooks params

I'm not sure what you meant by "creating reducers" but in this case only new actions would be added, what do you think?

@trevoro trevoro closed this as completed Jul 29, 2020
@trevoro trevoro reopened this Jul 29, 2020
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

No branches or pull requests

2 participants