reactjs / reactjs/react.dev

Aren't updater functions in state an antipattern?

Open
#1,717 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
11.8k
Forks
7.9k
Avg merge
1d 11h
Merged PRs (30d)
11

Description

In documentation here:
https://reactjs.org/docs/context.html#updating-context-from-a-nested-component

An updater function is placed directly in the state object for the sole purpose of passing it down through context:

this.state = {
  theme: themes.light,
  toggleTheme: this.toggleTheme,
};

This seems like an odd antipattern. I've never placed a method in state before. Shouldn't state be exclusively for properties that affect rendering and change over time?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review the linked React Context documentation, especially the “Updating Context from a Nested Component” section and its updater-in-state example. Determine whether the example needs clarification or revision, and consider the work complete when the documentation gives a clear, agreed explanation of this pattern.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
documentation
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.