Skip to content

Deprecate replaceState #3236

@sebmarkbage

Description

@sebmarkbage

The use cases for replaceState seems to fit one of these patterns:

  1. Let state temporarily be null until some data is fetched. Then reset it to null using replaceState at some later point.

  2. Use an immutable-js Record to store your state. That way you can use it with cursors and convenience methods like "update" and batching.

  3. Using immutable-js Map to store your data.

  4. Use state as a map. I.e. adding new keys dynamically. setState({ [someID]: value })

All these use cases break down when you need to add another state value which makes refactoring a pain when you thought that you wouldn't need to.

It also means that mixins can't add state. (I'm not particularly found of this pattern but it is a common one.)

We could potentially add native support for (2) but it would expand the record type to one that contains all the fields required by mixins.

We would like to add a warning if you expand the state with fields that are not in the original getInitialState. The reason is that VMs optimize around records that keep their type signature. By changing the type signature dynamically you introduce performance penalties. We can't add that warning if patterns 3-4 is used.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions