Skip to main content

Events and Listeners

Events are typically handled using React's useEffect hook.

useEffect

React has a tutorial as well as documentation that can be found here.

Use effect allows you to run a function every time a state or context property changes.​

The following example will run a function every time the user object is updated,

useEffect(() => { if (!!user) { // do something here } }, [user]);

Lifting Up State

In some cases, you may not even need to use useEffect.

You can lift up state as described in this article.

When a parent component is controlling state, and passing in handler functions as props to children components, may eliminate the need for an event altogether.