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

Popover trigger children component mount twice when in StrictMode #18083

Closed
hugo972 opened this issue Oct 29, 2019 · 2 comments
Closed

Popover trigger children component mount twice when in StrictMode #18083

hugo972 opened this issue Oct 29, 2019 · 2 comments
Labels
component: Popover The React component.

Comments

@hugo972
Copy link

hugo972 commented Oct 29, 2019

useState(() => console.log("debug")); just for ease of repro called twice when in StrcitMode

function Repro() {
    const [open, setOpen] = useState(false);
    return (
        <div>
            <Button onClick={() => setOpen(true)}>open</Button>
            <Popover open={open} onClose={() => setOpen(false)}><Debug/></Popover>
        </div>);
}

function Debug() {
    useState(() => console.log("debug"));
    return <div />;
}

ReactDOM.Render(<React.StrictMode><Repro /></React.StrictMode>);
  • [x ] The issue is present in the latest release.
  • [x ] I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

The console.log in the useState inital value callback should only be executed once. But when you open the popover you get two messages in the console. This behavior is consistant with all other hooks. Which lead me to guess there is a duplicate copy of the popover children running in parallel. This will only happen in StrcitMode. If you render in regular mode it will only console.log once.

Expected Behavior 🤔

Only a single copy of the children will be created (if that's the issue). console.log should be called only once.

Steps to Reproduce 🕹

run the code above and look in the console

Context 🔦

Your Environment 🌎

Repro using MaterialUI code sandbox example for popover https://codesandbox.io/s/p6i6p,
just add the <StrictMode> around <Demo/> and add the <Debug/> component under the popover and look in the console

@eps1lon
Copy link
Member

eps1lon commented Oct 29, 2019

This is expected behavior for StrictMode which calls all the methods that should be pure twice. This includes render methods and state initializers used in useState. https://reactjs.org/docs/strict-mode.html#detecting-unexpected-side-effects isn't very explicit about this though.

Aside: The linked demo does not use StrictMode.

@eps1lon eps1lon closed this as completed Oct 29, 2019
@hugo972
Copy link
Author

hugo972 commented Oct 29, 2019

Good to know, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: Popover The React component.
Projects
None yet
Development

No branches or pull requests

3 participants