Skip to content

Commit 66a1546

Browse files
committed
Add Pairing A Callback With A useState Hook as a react til
1 parent b207b9e commit 66a1546

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
1010
For a steady stream of TILs from a variety of rocketeers, checkout
1111
[til.hashrocket.com](https://til.hashrocket.com/).
1212

13-
_838 TILs and counting..._
13+
_839 TILs and counting..._
1414

1515
---
1616

@@ -562,6 +562,7 @@ _838 TILs and counting..._
562562
- [Mapping Over One Or Many Children](react/mapping-over-one-or-many-children.md)
563563
- [Mock A Function That A Component Imports](react/mock-a-function-that-a-component-imports.md)
564564
- [Navigate With State Via @reach/router](react/navigate-with-state-via-reach-router.md)
565+
- [Pairing A Callback With A useState Hook](react/pairing-a-callback-with-a-usestate-hook.md)
565566
- [Passing Props Down To React-Router Route](react/passing-props-down-to-react-router-route.md)
566567
- [Prevent reach/router Redirect Error Screen In Dev](react/prevent-reach-router-redirect-error-screen-in-dev.md)
567568
- [Proxy To An API Server In Development With CRA](react/proxy-to-an-api-server-in-development-with-cra.md)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Pairing A Callback With A useState Hook
2+
3+
React's Class-based state management allowed you to update the state of your
4+
component with a call to `this.setState()`. The first argument represents the
5+
changes to the state. It also accepts a second argument; a callback that will
6+
be invoked after the state has been updated.
7+
8+
```javascript
9+
this.setState({ loading: true }, () => console.log("Loading..."));
10+
```
11+
12+
If you've transitioned to Hooks-based state management, then you may have
13+
noticed that the updaters generated by `useState` calls do not accept a second
14+
callback argument.
15+
16+
If you want to update state and fire a callback in response to it, you can pair
17+
`useState` with `useEffect`.
18+
19+
```javascript
20+
import React, { useState, useEffect } from "react";
21+
22+
function App() {
23+
const [loading, setLoading] = useState(false);
24+
const toggleLoading = () => setLoading(prevLoading => !prevLoading);
25+
useEffect(() => {
26+
if(loading) {
27+
console.log("We are loading now");
28+
}
29+
}, [loading])
30+
31+
return (
32+
<div>
33+
{loading && <p>Loading...</p>}
34+
<button onClick={toggleLoading}>{loading ? "Cancel" : "Save"}</button>
35+
</div>
36+
);
37+
}
38+
```
39+
40+
The `useState` acts on its own. It has no side-effects. We follow it with a
41+
`useEffect` that responds to changes to the value of `loading` -- this is where
42+
our _callback_ gets invoked.
43+
44+
See a [live example](https://codesandbox.io/s/clever-roentgen-kvzze).

0 commit comments

Comments
 (0)