Skip to content

getDerivedStateFromProps not called before each render  #30

Open
@jiangyijie27

Description

@jiangyijie27

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

When I use React and [email protected], getDerivedStateFromProps will be called everytime before the render method.
image

but when I use React and [email protected], 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 [email protected], It doesn't work as I expected.

Thanks for helping.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions