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

useSessionStorageValue same key, different value #451

Closed
JaminFarr opened this issue Nov 12, 2021 · 5 comments · Fixed by #960
Closed

useSessionStorageValue same key, different value #451

JaminFarr opened this issue Nov 12, 2021 · 5 comments · Fixed by #960
Assignees
Labels

Comments

@JaminFarr
Copy link

Prior Issues

I cannot see any related issues.

What is the current behavior?

Different instances of useSessionStorageValue with the same key that are mounted at the same can get out of sync when setValue is use on first mount within a useEffect. Only the component making the setValue call, and components before it, get the updated value. Components after do not update and have a stale value.

Steps to Reproduce

Using setValue in a useEffect(..., []).

const useStoredValue = () =>
  useSessionStorageValue("example-key", "Default Value");

const UpdateOnMount = () => {
  const [, setValue] = useStoredValue();

  useEffect(() => {
    setValue("Update Value");
  }, [setValue]);

  return null;
};

const DisplayValue = () => {
  const [value] = useStoredValue();

  return <p>Stored value is "{value}"</p>;
}

const App = () => (
  <div>
    <DisplayValue /> 
    <UpdateOnMount />
    <DisplayValue />
  </div>
)

codesandbox: https://codesandbox.io/s/eager-gould-ter7m?file=/src/App.js

The code above on first load displays:

Stored value is "Update Value"

Stored value is "Default Value"

Another codesandbox with random values and reset buttons: https://codesandbox.io/s/fervent-stitch-zqekk

There's a few work arounds:

  • Changing the component order
  • Using initializeWithStorageValue: false
  • Putting setValue in a setTimeout
  useEffect(() => {
    setTimeout(() => {
      setValue("Update Value");
     }, 0);
  }, [setValue]);

What is the expected behavior?

All instances of useSessionStorageValue with the same key should return the same value.

Environment Details

  • @react-hookz/web version: 12.0.0
  • react version: 17.0.2
  • react-dom version: 17.0.2
  • typescript version: N/A
  • OS: macOS BigSur
  • Browser: Chrome 95 / Firefox / Safari
  • Did this work in previous versions? No know
@JaminFarr JaminFarr added the 🪲 bug Something isn't working label Nov 12, 2021
xobotyi added a commit that referenced this issue Dec 23, 2021
github-actions bot pushed a commit that referenced this issue Dec 23, 2021
## [12.0.3](v12.0.2...v12.0.3) (2021-12-23)

### Bug Fixes

* **451:** attempt to fix issue [#451](#451) ([#539](#539)) ([c2f99da](c2f99da))
@xobotyi
Copy link
Contributor

xobotyi commented Dec 23, 2021

🎉 This issue has been resolved in version 12.0.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

@xobotyi xobotyi reopened this Dec 23, 2021
@xobotyi
Copy link
Contributor

xobotyi commented Dec 23, 2021

Well, nope, didnt worked out. Ill dig more later.

@ArttuOll
Copy link
Contributor

ArttuOll commented Oct 8, 2022

I'm investigating this also. Will report here if I find anything out.

@ArttuOll
Copy link
Contributor

ArttuOll commented Oct 8, 2022

I can confirm that:

  • This affects both useLocalStorage and useSessionStorage (which is not surprising, since both use useStorage under the hood).
  • This bug has been present since useLocalStorage and useSessionStorage were first added to this library.

ArttuOll added a commit to ArttuOll/react-hookz-web that referenced this issue Oct 8, 2022
…s not mounted

Using normal useState to store the value of the tracked key instead of useSafeState seems to solve
the problem described in react-hookz#451.

fix react-hookz#451
ArttuOll added a commit to ArttuOll/react-hookz-web that referenced this issue Oct 9, 2022
…s not mounted

Using normal useState to store the value of the tracked key instead of useSafeState seems to solve
the problem described in react-hookz#451.

fix react-hookz#451
@xobotyi
Copy link
Contributor

xobotyi commented Nov 3, 2022

🎉 This issue has been resolved in version 17.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

3 participants