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

GlobalState doesn't emit when trying to set state back to initialState #50

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

philiptzou
Copy link

@philiptzou philiptzou commented Apr 14, 2021

Minimal code for reproducing the problem:

import React from 'react';
import createPersistedState from 'use-persisted-state';

const useFlag = createPersistedState("flag");

function Inner() {
  const defaultFlag = false;
  const [flag, setFlag] = useFlag(defaultFlag);

  return <div onClick={handleClick}>{flag.toString()}</div>;

  function handleClick() {
    setFlag(!flag);
  }
}

export default function Outer() {

  return <div>
    <Inner key={1} />
    <Inner key={2} />
    <Inner key={3} />
  </div>;
}

The code works well when the local storage variable "flag" was set to false. However, when "flag" was set to true, clicking one of the <Inner /> element doesn't change the flag state of other <Inner />'s. Because GlobalState compares the new value to the defaultFlag, but not to the variable "flag" from the local storage.

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.

1 participant