Props 和 State

在 React 中,Props 和 State 是两个非常重要的概念,它们是 React 组件用来管理和传递数据的两种主要方式。理解这两个概念对于掌握 React 开发至关重要。

1. Props

1.1 什么是 Props?

Props(Properties 的缩写)是 React 组件之间传递数据的一种方式。它们是只读的,这意味着你不能在组件内部修改 props。Props 通常用于父组件向子组件传递数据。

1.2 使用 Props

1.2.1 传递 Props

在父组件中,你可以通过在子组件标签中添加属性来传递 props。

import React from 'react';

function ParentComponent() {
  return (
    <ChildComponent name="Alice" age={25} />
  );
}

function ChildComponent(props) {
  return (
    <div>
      <p>Name: {props.name}</p>
      <p>Age: {props.age}</p>
    </div>
  );
}

export default ParentComponent;

在这个例子中,ParentComponent 通过 props 向 ChildComponent 传递了 name 和 age 两个属性。

1.2.2 接收 Props

在子组件中,你可以通过 props 对象来接收这些数据。

function ChildComponent(props) {
  return (
    <div>
      <p>Name: {props.name}</p>
      <p>Age: {props.age}</p>
    </div>
  );
}

1.3 实践练习

创建一个 Greeting 组件,它接收 name 作为 prop,并显示一条问候信息。

import React from 'react';

function Greeting(props) {
  return <h1>Hello, {props.name}!</h1>;
}

function App() {
  return (
    <div>
      <Greeting name="Alice" />
      <Greeting name="Bob" />
    </div>
  );
}

export default App;

2. State

2.1 什么是 State?

State 是 React 组件内部管理的数据。与 props 不同,state 是可变的,这意味着你可以在组件内部修改它。State 通常用于管理组件的内部状态,例如表单输入、用户交互等。

2.2 使用 State

2.2.1 初始化 State

在类组件中,你可以通过 this.state 来初始化 state。

import React, { Component } from 'react';

class Counter extends Component {
  constructor(props) {
    super(props);
    this.state = {
      count: 0
    };
  }

  render() {
    return (
      <div>
        <p>Count: {this.state.count}</p>
      </div>
    );
  }
}

export default Counter;

2.2.2 更新 State

你可以使用 this.setState() 方法来更新 state。注意,setState 是异步的,因此如果你需要基于当前 state 计算新的 state,你应该传递一个函数而不是一个对象。

class Counter extends Component {
  constructor(props) {
    super(props);
    this.state = {
      count: 0
    };
  }

  increment = () => {
    this.setState({ count: this.state.count + 1 });
  }

  render() {
    return (
      <div>
        <p>Count: {this.state.count}</p>
        <button onClick={this.increment}>Increment</button>
      </div>
    );
  }
}

2.3 实践练习

创建一个 Toggle 组件,它有一个按钮,点击按钮时切换显示 "ON" 或 "OFF"。

import React, { Component } from 'react';

class Toggle extends Component {
  constructor(props) {
    super(props);
    this.state = {
      isToggleOn: true
    };
  }

  handleClick = () => {
    this.setState(prevState => ({
      isToggleOn: !prevState.isToggleOn
    }));
  }

  render() {
    return (
      <button onClick={this.handleClick}>
        {this.state.isToggleOn ? 'ON' : 'OFF'}
      </button>
    );
  }
}

export default Toggle;

3. Props 和 State 的区别

  • Props 是只读的,用于从父组件向子组件传递数据。
  • State 是可变的,用于管理组件的内部状态。

4. 总结

Props 和 State 是 React 中管理数据的两种主要方式。Props 用于组件之间的数据传递,而 State 用于管理组件的内部状态。理解这两个概念对于构建动态和交互式的 React 应用至关重要。

5. 下一步

在掌握了 Props 和 State 之后,你可以继续学习 React 的生命周期方法、事件处理、条件渲染等内容,逐步深入 React 的开发。


通过本教程,你应该已经掌握了 Props 和 State 的基本概念和使用方法。接下来,你可以通过实践练习来巩固这些知识,并开始构建更复杂的 React 应用。