Share
what is diff b/w stateless(Functional) and stateful(Class) components in react?
Question
- A stateless component is usually associated with how a concept is presented to the user.
- It is similar to a function in that, it takes an input (props) and returns the output (react element).
- A stateful component is always a class component. It is created by extending the React.Component class.
- A stateful component is dependent on its state object and can change its own state. The state gets initialized in the constructor. The component re-renders based on changes to its state and may pass down properties of it’s state to child components as properties on a props object.
When would you use a stateless component?? - When you just need to present the props.
- When you don’t need a state or any internal variables.
- When creating element does not need to be interactive.
- When you want reusable code.
When would you use a stateful component? - When building element that accepts user input…or element that is interactive on-page.
- When dependent on the state for rendering, such as fetching data before rendering.
- When dependent on any data that cannot be passed down as props.
Leave an answer