From b78239be2b3eb5c1b3233c2f8abce4551dd8b0cd Mon Sep 17 00:00:00 2001 From: Aaron Gottlieb Date: Wed, 5 Dec 2018 16:28:22 -0700 Subject: [PATCH] Example of setState callback (#29836) --- guide/english/react/state/index.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/guide/english/react/state/index.md b/guide/english/react/state/index.md index 248bc1267d..eed6dbc435 100644 --- a/guide/english/react/state/index.md +++ b/guide/english/react/state/index.md @@ -111,6 +111,15 @@ this.setState(increment) Importing state via changes via functions helps keep each module as short as possible and can be used for cleaner code. +#### setState's callback function +the `setState` method for class based React components also takes a second argument: a callback function that is called once state is successfully updated. This can be useful for performing tasks that rely on the most recent state value (but aren't in the render method). +``` +this.setState({ friends: 0}, () => this.findNewFriends()); +... + +findNewFriends = (friends = this.state.friends) => friends < 1 ? this.props.dispatch(getFriendSuggestions()) : this.props.dispatch(getCurrentChatRecords()); +``` + #### More Information - [React - State and Lifecycle](https://reactjs.org/docs/state-and-lifecycle.html)