Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reactjs: What's best practice for working with class vs functional components?

Tags:

reactjs

So both class-based and functional components can work with state and lifecycle methods, if I am not mistaken. And that it is possible to structure your app with both or just functional components using hooks.

I was wondering if there are actually best practices in the real world. What approach is usually used in a corporate environment? Is there a definite answer to this or do companies combine all approaches constantly?

like image 732
flooz Avatar asked Oct 16 '22 10:10

flooz


1 Answers

Using class components is "mildly discouraged". Which means writing new code mostly using function components (unless there are compelling reasons not to) and not spending any time/resourses on rewriting existing codebase to move from class components to their functional counterparts.

An example of a compelling reason to use class component would be the need to optimise the component that uses Redux (e.g. is connected to Redux store) for performance to the extent beyond what hooks allow.

like image 90
winwiz1 Avatar answered Oct 21 '22 08:10

winwiz1