Describe the bug
The documentation for args says that "When an arg鈥檚 value is changed, the component re-renders", but that does not seem to be what happens. Instead of re-rendering, the whole story/component appears to be re-instantiated from scratch, losing any existing state in the process.
To Reproduce
Steps to reproduce the behavior:
export const Test: Story<{ count: number }> = args => {
const [yes, setYes] = useState(false);
useEffect(() => {
console.log('Initalised');
}, []);
return (
<div>
<p>{yes ? 'Yes' : 'No'}</p>
<p>Count: {args.count}</p>
<p>
<button onClick={() => setYes(!yes)}>Toggle</button>
</p>
</div>
);
};
Test.args = {
count: 0,
};
count arg.yes gets reset to false.Expected behavior
The story should retain the value of yes as true. The init effect should only be run once, when the story is first loaded.
Code snippets
See above.
System
Environment Info:
System:
OS: macOS
Binaries:
Node: 13.8.0 - ~/.nvm/versions/node/v13.8.0/bin/node
Yarn: 1.22.10 - /usr/local/bin/yarn
npm: 6.13.6 - ~/.nvm/versions/node/v13.8.0/bin/npm
Browsers:
Chrome: 86.0.4240.111
Firefox: 82.0
Safari: 14.0
Additional context
Add any other context about the problem here.
cc @tmeasday
@jonrimmer are you using any decorators which render the story via JSX? (<StoryFn />).
If so this is a dupe of https://github.com/storybookjs/storybook/issues/12255 and a workaround is to just use StoryFn().
@tmeasday Ah, yes, we are. That'll be it then. Thanks.
Ok, I'm going to close this (and look at that issue again really soon!)
@tmeasday this seems to be an issue without decorators. Here is some logging in the react-ts-essentials example:
https://github.com/morgs32/storybook/tree/next/examples/cra-ts-essentials
When you call reactDOM.render() with new args it doesn't automatically dismount and remount?
https://github.com/storybookjs/storybook/pull/13069/files#diff-4ff35b6b89e790db308e2624d4187ab9a4cbc01bb54f92c0a0095f7b11a01189L10
@morgs32 it does too have a decorator ;):
When you call reactDOM.render() with new args it doesn't automatically dismount and remount?
Not if the component hasn't changed, no. Try it out in codesandbox!