Skip to content

The example in hooks-faq "is-there-something-like-forceupdate" is wrong #2410

Description

@jauco

Direct link: https://reactjs.org/docs/hooks-faq.html#is-there-something-like-forceupdate

The example says

const [ignored, forceUpdate] = useReducer(x => x + 1, 0);

  function handleClick() {
    forceUpdate();
  }

But this makes typescript report an error. (The typescript types are provided by the react team itself right? otherwise I might need to file a bug with the team maintaining the types)

Possible fixes:

const [ignored, forceUpdate] = useReducer(x => x + 1, 0);

  function handleClick() {
    forceUpdate("something"); //I added an argument here
  }

will pass the type checker

const [ignored, forceUpdate] = useState(0); //useState instead of useReducer

  function handleClick() {
    forceUpdate(x => x + 1); //calculate function
  }

is also ok (and a bit nicer IMHO)

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