Skip to content

getDerivedStateFromProps not called before each render  #30

@jiangyijie27

Description

@jiangyijie27

Hi,
I wrote a component Switch: https://codesandbox.io/s/m5oy50k649

When I use React and ReactDom@16.4.1, getDerivedStateFromProps will be called everytime before the render method.
image

but when I use React and ReactDom@15.0.0, and with this polyfill, getDerivedStateFromProps is called just before the first render, as my example shows. This is not what I expected.
image

React Doc:

getDerivedStateFromProps is invoked right before calling the render method, both on the initial mount and on subsequent updates

Here's what I want:
Switch has a state checked, and a prop checked as well.
if <Switch />, the component can work because it has it's own state checked;
if <Switch checked />, the component will never change state, because before every render, getDerivedStateFromProps will reset the components' checked.

But when React and ReactDom@15.0.0, It doesn't work as I expected.

Thanks for helping.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions