F
F
Fengol2019-04-01 22:09:11
JavaScript
Fengol, 2019-04-01 22:09:11

Is it possible to create a complete replacement for class components using hooks?

Until recently, I thought that hooks allow you to completely abandon class components and, in essence, are their "killer". And besides, creating your own hooks promises to make it easier to work with reusable code. I was happy living with this idea until I needed to move the logic of the service into a custom hook. The fact is that this service, in combination, is a separate library, which, in the best traditions, basically uses the event model, that is, it needs to be subscribed / unsubscribed, and in addition, it has its own life cycle with the obligatory destroy. You can subscribe, but how do you unsubscribe when there is no alternative to componentWillUnmount? The function that is returned from useEffect either fires every update, and in addition, the destroy call constantly resets the state of the service. Is it again cheating on the part of react? Is it really cool again only for helloworld applications on ready-made ui libraries? The whole point of these code reuses through hooks should be the basis for helping you create your own complex components. And here again an ambush?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-04-01
@Fengol

useEffect(() => {
  // component did mount code here
  return () => {
    // component will unmount code here
  };
}, []);

Read.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question