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

Update README.md #263

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 24 additions & 7 deletions README.md
Expand Up @@ -773,15 +773,32 @@

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.

The callback function as an argument of setState() is used to perform actions after the state has been updated. This is because setState() is an asynchronous function, which means that the state update does not happen immediately. Instead, the state update is scheduled to happen later, and the callback function will be called once the update has completed.

**Note:** It is recommended to use lifecycle method rather than this callback function.
The callback function can be used to perform asynchronous actions after the state has been updated, such as performing a network request to fetch new data, updating the UI with the new state, or logging the state change.

In most cases, it is recommended to use the componentDidUpdate() lifecycle method instead of the callback function. This is because componentDidUpdate() is guaranteed to be called after the state has been updated, whereas the callback function may not be called if the state update is interrupted.

Here is an example of how to use the callback function with setState():

const [counter, setCounter] = useState(0);

const handleClick = () => {
setCounter((prevCounter) => {
return prevCounter + 1;
});
};

const render = () => {
return (
<div>
<button onClick={handleClick}>Click me</button>
<p>The counter is {counter}</p>
</div>
);
};

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

**[⬆ Back to Top](#table-of-contents)**

Expand Down