-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Add stable fn notes to useMemo, useTransition, useState, and useReducer #7181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1056,6 +1056,83 @@ Keep in mind that you need to run React in production mode, disable [React Devel | |
|
||
--- | ||
|
||
### Preventing an Effect from firing too often {/*preventing-an-effect-from-firing-too-often*/} | ||
|
||
Sometimes, you might want to use a value inside an [Effect:](/learn/synchronizing-with-effects) | ||
|
||
```js {4-7,10} | ||
function ChatRoom({ roomId }) { | ||
const [message, setMessage] = useState(''); | ||
|
||
const options = { | ||
serverUrl: 'https://localhost:1234', | ||
roomId: roomId | ||
} | ||
|
||
useEffect(() => { | ||
const connection = createConnection(options); | ||
connection.connect(); | ||
// ... | ||
``` | ||
|
||
This creates a problem. [Every reactive value must be declared as a dependency of your Effect.](/learn/lifecycle-of-reactive-effects#react-verifies-that-you-specified-every-reactive-value-as-a-dependency) However, if you declare `options` as a dependency, it will cause your Effect to constantly reconnect to the chat room: | ||
|
||
|
||
```js {5} | ||
useEffect(() => { | ||
const connection = createConnection(options); | ||
connection.connect(); | ||
return () => connection.disconnect(); | ||
}, [options]); // 🔴 Problem: This dependency changes on every render | ||
// ... | ||
``` | ||
|
||
To solve this, you can wrap the object you need to call from an Effect in `useMemo`: | ||
|
||
```js {4-9,16} | ||
function ChatRoom({ roomId }) { | ||
const [message, setMessage] = useState(''); | ||
|
||
const options = useMemo(() => { | ||
return { | ||
serverUrl: 'https://localhost:1234', | ||
roomId: roomId | ||
}; | ||
}, [roomId]); // ✅ Only changes when roomId changes | ||
|
||
useEffect(() => { | ||
const options = createOptions(); | ||
const connection = createConnection(options); | ||
connection.connect(); | ||
return () => connection.disconnect(); | ||
}, [options]); // ✅ Only changes when createOptions changes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// ... | ||
``` | ||
|
||
This ensures that the `options` object is the same between re-renders if `useMemo` returns the cached object. | ||
|
||
However, since `useMemo` is performance optimization, not a semantic guarantee, React may throw away the cached value if [there is a specific reason to do that](#caveats). This will also cause the effect to re-fire, **so it's even better to remove the need for a function dependency** by moving your object *inside* the Effect: | ||
|
||
```js {5-8,13} | ||
function ChatRoom({ roomId }) { | ||
const [message, setMessage] = useState(''); | ||
|
||
useEffect(() => { | ||
const options = { // ✅ No need for useMemo or object dependencies! | ||
serverUrl: 'https://localhost:1234', | ||
roomId: roomId | ||
} | ||
|
||
const connection = createConnection(options); | ||
connection.connect(); | ||
return () => connection.disconnect(); | ||
}, [roomId]); // ✅ Only changes when roomId changes | ||
// ... | ||
``` | ||
|
||
Now your code is simpler and doesn't need `useMemo`. [Learn more about removing Effect dependencies.](/learn/removing-effect-dependencies#move-dynamic-objects-and-functions-inside-your-effect) | ||
|
||
|
||
### Memoizing a dependency of another Hook {/*memoizing-a-dependency-of-another-hook*/} | ||
|
||
Suppose you have a calculation that depends on an object created directly in the component body: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is leftover from the
useCallback
example.