What are Effects and how are they different from events?

Before getting to Effects, you need to be familiar with two types of logic inside React components:

Sometimes this isn’t enough. Consider a ChatRoom component that must connect to the chat server whenever it’s visible on the screen. Connecting to a server is not a pure calculation (it’s a side effect) so it can’t happen during rendering. However, there is no single particular event like a click that causes ChatRoom to be displayed.

<aside> 💡 Effects let you specify side effects that are caused by rendering itself, rather than by a particular event

</aside>

Sending a message in the chat is an *event*

However, setting up a server connection is an *Effect*

You might not need an Effect

Don’t rush to add Effects to your components.

If your Effect only adjusts some state based on other state, you might not need an Effect.

How to write an Effect

To write an Effect, follow these three steps:

  1. Declare an Effect.

    — By default, your Effect will run after every render.