Data in React flows in a one-way direction from top to bottom, that is, from the parent component to the child component. The component's data is stored in props and state. In any application, data is indispensable. We need to directly change a section of the page to refresh the view, or indirectly modify data in other places. In React, we use the props and state attributes to store data.
The main purpose of state is to allow the component to save, control, and modify its mutable state. state is initialized within the component and can be modified by the component itself, and cannot be accessed or modified externally. state can be thought of as a local data source that can only be controlled by the component itself. The state can be updated using the this.setState method, which triggers the component to re-render.
The main purpose of props is to allow the parent component using the component to pass parameters to configure the component. It is a configuration parameter passed in from the outside, and cannot be controlled or modified internally. The component's props will remain unchanged unless new props are actively passed from the external component.
Both state and props can determine the behavior and display of the component. Data from a component's state can be passed to a child component through props. A component can use externally passed props to initialize its own state. However, their responsibilities are very clear: state allows the component to control its own state, while props allows external configuration of the component. In simple terms, props are passed to the component (similar to function parameters), while state is managed by the component itself internally (similar to a variable declared within a function).
A clear principle is to use state as little as possible and props as much as possible. A component without state is called a stateless component, while one with state is called a stateful component. Because state brings management complexity, we should write more stateless components and fewer stateful components, which will lower the difficulty of code maintenance and enhance component reusability to some extent.
The core idea of React is the componentization concept, where the page is divided into independent and reusable components. Conceptually, a component is a function that can accept a parameter as an input, and this parameter is the props. Therefore, props can be understood as data passed from the outside to the inside of the component. As React follows a one-way data flow, props is basically data passed from a parent component to a child component.
Suppose we need to implement a list, where each row in the list is a component. This means we have two components, <ItemList/> and <Item/>. For the ItemList component, we temporarily assume that the list's data is stored in a data variable, and then use the map function to return an array, where each item is <Item item={data}/>. Essentially, this includes data.length <Item/> components, and data is passed to the component as a custom parameter. Inside the Item component, this.props is used to access all the data passed to it as an object. Currently, it only includes an item property, so it can be accessed using this.props.item.
props are often used for rendering components and initializing state. Once a component is instantiated, its props are read-only and cannot be changed. If props can be changed during the rendering process, it will lead to an unpredictable appearance of the component. New props can only be passed into the component through the parent component's re-rendering process. In other words, props are parameters passed into the component from the outside and primarily act as a means to transfer data from the parent component to the child component. They are both readable and immutable, and new props can only be passed in by the parent component to re-render the child component, otherwise the child component's props and display will not change.
In a component, we can also set a defaultProps for the parameters in props, and specify their types.
The different validator types are as follows.
One difference between state and props is that state can be changed. However, it cannot be directly modified using this.state= values; and needs to be modified using the this.setState() method. For example, we often need to perform asynchronous operations to fetch data, which should be executed in the didMount lifecycle stage.
When we call the this.setState method, React updates the component's data state state and re-invokes the render method, which means the component will be re-rendered. setState accepts an object or a function as the first parameter, and only the part that needs to be updated needs to be passed in. setState can also accept a second parameter, which is a function that will be called when setState is completed and the component starts to re-render, and can be used to monitor the completion of rendering.