cerebral / overmind

Overmind - Frictionless state management
https://overmindjs.org
MIT License
1.58k stars 95 forks source link

[BUG] React component doesn't rerender on state change #524

Closed jmattheis closed 2 years ago

jmattheis commented 3 years ago

In the example below overmind won't rerender SubComponent on state changes. When adding a useOvermindState all works fine. I guess it is because of the WrapChildren component. Overmind rerenders the WrapChildren component but this dosen't update children from the parent component which results in outdated values.

const WrapChildren: React.FC = ({ children }) => {
  const state = useOvermindState();

  return (
    <div>
      {state.demo.other}
      {children}
    </div>
  );
};

const SubComponent = ({
  value,
  setValue
}: {
  value: DemoState;
  setValue: (v: string) => void;
}) => {
  // with this everything works fine
  // useOvermindState()
  return (
    <div>
      <input value={value.name} onChange={(e) => setValue(e.target.value)} />
    </div>
  );
};

export const App = () => {
  const actions = useOvermindActions();
  const state = useOvermindState();

  return (
    <>
      <WrapChildren>
        <SubComponent
          value={state.demo}
          setValue={(newValue) => actions.demo.setName(newValue)}
        />
      </WrapChildren>
    </>
  );
};

In the example / CodeSandbox the it is not possible to write in the text input, because it doesn't receive the newer values.

CodeSandbox: https://codesandbox.io/s/overmind-no-react-rerender-k98d4

grandevel commented 2 years ago

Going through and cleaning up issues. Looking at this, I think everything is behaving as expected:

To see this for yourself, just add a console.log('render mycomp' ) to both the WrapChildren and Subcomponent, and you'll see which is re-rendering on text box change/action call. React does not re-render Sub even though it re-renders Wrap.

Closing this issue but reopen if you disagree?