me.at.coding
me.at.coding

Reputation: 17604

What can't be done without useEffect hook?

I read the docs about useEffect and understand how it makes life more easier than life cycle methods. Yet I am wondering what would not be possible without useEffect?

E.g. instead of (all code is dummy code)

useEffect(networkRequest.then(update state));

couldn't I just use

// inside functional component
function App() {
  networkRequest.then(update state)
}

Upvotes: 0

Views: 230

Answers (1)

Drew Reese
Drew Reese

Reputation: 202608

What do you mean "makes life more easier than lifecycle methods"? React hooks are how you utilize component lifecycle in functional components.

Just like the render lifecycle method of class-based components is to be a pure function without side-effects, so to is the entire body of a functional component. The body of a functional component is the "render" function.

// inside functional component
function App() {
  networkRequest.then(update state)
}

The above, as written, has no guard protecting the side-effect of the network request nor state update... it will simply update state and rerender, and update state and rerender, ... ad nauseam, or in other words, infinitely render loop.

React hooks are what allow you to utilize component lifecycle in functional components.

To directly answer your question though:

Yet I am wondering what would not be possible without useEffect?

It would not be possible to trigger side-effects such as any asynchronous network requests, authentication, etc... It wouldn't be possible to invoke any specific when a component mounts or rerenders. It wouldn't be possible to run any specific logic when a specific state/prop value updated.

Upvotes: 1

Related Questions