Home  >  Article  >  Web Front-end  >  What are the react state components?

What are the react state components?

青灯夜游
青灯夜游Original
2022-03-22 14:16:501440browse

There are two types of react state components: 1. Stateful components are components that can define state and are used where data needs to be changed; 2. Stateless components are components that cannot define state and are generally used in There is no data change per se.

What are the react state components?

The operating environment of this tutorial: Windows7 system, react17.0.1 version, Dell G3 computer.

React components: stateful components and stateless components

1. What is component state

Definition: It is data used to describe the shape of a transaction at a certain moment. Generally written as state.

Features: The state can be changed, and the view will change accordingly

Function: (1) Save data (2) Lay the foundation for subsequent update of the view

For example , design a counter. After clicking the 1 button, the number of the counter will change to the original number 1

2. Two states

## 2.1 Stateless components: Components that cannot define state. Function components are also called stateless components

Stateless components are generally used in applications where there is no data change. Where, such as rendering a piece of product introduction text, it does not need to be updated in real time. Its biggest advantage is that it can be reused at any time

2.2 Stateful component: A component that can define state,Classic components are also called stateful components

The application scenarios of stateful components are much wider, and they can be found in basically all places where data needs to be changed.

3. Class component status instance

import React from "react";
export default class Hello extends React.Component {
  // 这里的state就是状态
  state = {
    list: [{ id: 1, name: "明天会更好" },{ id: 2, name: "难忘今宵" }],
    isLoading: true
  };
}
 
  render() {
//如果当state里的数据种类较多时可以使用解构赋值
// 例如:const { tabs, active, list, content } = this.state
    return (
      <>
        

歌单-{this.state.count}

    {this.state.list.map((item) => (
  • {item.name}
  • ))}
{this.state.isLoading ? "正在加载" : "加载完成"}
); }

[Related recommendations:

Redis video tutorial]

The above is the detailed content of What are the react state components?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn