DEV Community

Cover image for React.js ~The principle of useEffect ~
Ogasawara Kakeru
Ogasawara Kakeru

Posted on

React.js ~The principle of useEffect ~

I think the princile of React is that React is a UI library. React manages UI, but other roles. A developer who thinks React is difficult does everything in React. Application logic or state management is not React's role. You should manage them by using a third-party library such as Zustand.

This statement can be applied to useEffect. useEffect is used to manage UI, not others.

Another princile is that React is a component based lirary. React maneges UI through a component. So, codebases in a component should be maneged in the component including useEffect. In other words, itโ€™s not a good idea to write logic that isnโ€™t confined to that component, and useEffect is no exception.

In React, a component is by turns mounted and unmounted. When a conponent is unmounted, The effect of the componen that the component affected UI must be disappered, because the component disappers. Because of that, as a principle of coding, it can be said that useEffect without cleanup function is invalid. useEffect without cleanup function doesn't restore the effect of the component. This breaks the principle of the component based coding.

Top comments (0)