Skip to content
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

Fixed the issue of #254 #256

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -773,14 +773,36 @@

12. ### What is the purpose of callback function as an argument of `setState()`?

The callback function is invoked when setState finished and the component gets rendered. Since `setState()` is **asynchronous** the callback function is used for any post action.
`SetState` is an `asynchronous method`. `Asynchronous` means that the remaining code will get executed while the current action is being performed. Whereas `synchronous` code will block the code execution while the current action is being performed. So, if you want an action to be performed only after the state has been updated you can make use of a call back function. This `callback function` is put as an argument to setstate method. This is the exact purpose of defining a callback function as an argument of setState.

**Note:** It is recommended to use lifecycle method rather than this callback function.

```javascript
setState({ name: "John" }, () =>
console.log("The name has updated and component re-rendered")
);
```jsx
import React from "react";

class App extends React.Component {
constructor() {
super();
this.state = {
value: 0
}
}

call() {
this.setState({ value: this.state.value + 1 }, () =>
console.log("Updated Value :" + this.state.value)
);
}

render() {
return (
<div>
<button onClick={() => { this.call() }}>
Click to update state!
</button>
</div>
);
}
}
export default App;
```

**[⬆ Back to Top](#table-of-contents)**
Expand Down Expand Up @@ -4920,7 +4942,7 @@
You should be passing the numbers via curly braces({}) where as strings in quotes

```jsx
React.render(
React.render(
<User age={30} department={"IT"} />,
document.getElementById("container")
);
Expand Down