When I using function component and when I using class component in react because when I work with react.js I can't understand what difference because the function and class component is work fine
-
Basic explanation - geeksforgeeks.org/…Jayant Varshney– Jayant Varshney2020-12-14 10:19:10 +00:00Commented Dec 14, 2020 at 10:19
2 Answers
Shortly, Class Components:
- Have their own state to implement some logic to your app;
- Have Lifecycle methods (
componentDidMount(),componentDidUpdate(), etc);
Functional Components are simple JS functions that accept props and return a React element, without any changes.
Comments
Nowadays the differences of use between class based components and functional ones are very few.
Just a little implementation to what @airblade said: while class components have a state and Lifecycle methods, from October 2018 (I might be wrong on the date), functional components have the so called hooks: functions that simulate state or Lifecycle methods for functional components.
Personally I only use functional components since I have recently started programming in React and looks like that's where React is heading to.
There's this good article on Medium that explains the use of React Hooks: React Hooks — How To Use useState and useEffect Example