Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

How to use stateless components in reactjs?


Asked by Marlowe Cisneros on Dec 10, 2021 FAQ



First: Consider to use Stateless Functional Components instead of Stateful Components in cases like your "Class1" that don't use states, only props.
Indeed,
Stateless component: 1 A component that has no internal state management in it. 2 In some component, the data remains the same, for example, showing the static data. 3 Function components are simply functions that receive the props and return the JSX code. 4 Stateless components can not use the react life cycle hooks More items...
Also, The state gets initialized in the constructor. It stores information about the component’s state change in memory. It may get changed depending upon the action of the component or child components. Stateless components are those components which don’t have any state at all, which means you can’t use this.setState inside these components.
Besides,
Avoid creating stateful components to avoid unnecessary complexity when it is possible. Unlike a component's state, props give components the ability to receive data from a parent component when the content inside the component needs to be changed. Most components will receive props and render a React element.
Next,
Different component classifications have been covered such as class vs. functional components, stateful vs. stateless components, and container vs. presentational components. Before getting started, I want to introduce you to the code snippet that we will be using in this tutorial. It is a simple counter built with React.