Skip to content

[Hooks] [Docs] Warn against mutating state in useState #1646

Closed
@sudhirj

Description

@sudhirj

Given this change facebook/react#14752 mutating state (arrays or objects) no longer works at all. New objects must be constructed every time with Object.assign, or if arrays are used they must be cloned, mutated and returned.

Examples of doing that with both objects and arrays would be helpful, especially with the form that takes a function:

const [list, setList] = useState([])
setList((l) => {
  l.push(1)
  return l
}

https://twitter.com/sudhirj/status/1093586382713348096

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