2 posts tagged with "hook"

View All Tags

React Hooks Cheat Sheet

Bunlong

Bunlong

React Patterns Team

React Hooks Cheat Sheet

State Management#

useState()#

Declare state.

const [name, setName] = useState('Initial value');

Update state.

setName('New value'); // directly
setName((value) => 'New' + value); // based on previous state

How to use async function in useEffect hook?

Bunlong

Bunlong

React Patterns Team

How to use async function in useEffect hook?

How to use async function in useEffect hook?#

At first glance you will have an idea as following.

useEffect(async () => {
await someTask();
}, []);

What's wrong?

The compiler should be yielding something as following.

Argument of type '() => Promise<void>' is not assignable to parameter of type 'EffectCallback'.