C
C
ch-aqwer2019-11-04 13:35:58
JavaScript
ch-aqwer, 2019-11-04 13:35:58

When to use a class in react 16.*?

Good afternoon! Please tell me, is it worth using class in react 16. * or is it better to avoid them already? Since there are all sorts of hooks plus it is already possible to use connect with functional components, this question arose ... It is
interesting to listen to the opinions of experienced developers)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Dozmorov, 2019-11-04
@ch-aqwer

You can not avoid it, but on hooks it turns out to be cleaner and more modern) the only thing missing in hooks is an analogue of componentDidUpdate, for situations where you need to execute code always, but not during component mounting. This can be bypassed with useRef.

const isMountRef = useRef(false);

useEffect(() => {
    if (isMountRef.current) {
      // some code
    }
  }, [someDependencies]);

useEffect(() => {
    // Using for useEffect on didUpdate (not mount)
    isMountRef.current = true;
  }, []);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question