美文网首页
Redux——React 组件中的数据传输

Redux——React 组件中的数据传输

作者: CharleyChai | 来源:发表于2019-02-28 22:36 被阅读0次

React中的数据有两种:prop和state。其中prop负责对外的数据交互,state负责内部的数据管理。

React中的prop

prop是property的简写,一个React组件通过定义prop来确定自己对外的数据接口,外部环境就通过prop来与组件进行交流。

1. 组件通过prop与外界交互

假如说有一个我们自定义的组件<MyComp>,那么我们在使用这个组件的时候就可以按照如下的方式来通过prop向该组件传入数据:

<MyComp name="charley" class="btn" onClick={onBtnClick}></MyComp>

其中的nameclassonClick就是prop。可以看到prop的类型还可以是函数,组件可以通过调用类型为函数的prop来将一些信息反馈给外界。

这里还需要注意一点,prop应该由外部组建设置,本组件并不能对其进行修改。

2. 组件使用prop

<MyComp>组件内部如下对prop进行使用

class MyComp extends Component {
  constructor () {
    super()
    // ...
  }
  // ...
  render () {
    return (
      <div onClick={ this.props.onClick.bind(this) }>
        <span>{ this.props.name }</span>
      </div>
    )
  }
}

React中的State

state代表的是内部的状态,例如我们开发一个有计数器的组件,其所记的数就可以作为一个state。在使用state的时候,我们首先要对其进行初始化,在对其进行更新的时候,使用setSate函数进行。

1. 初始化state

state的初始化在constructor()函数中完成。

constructor() {
  super()
  this.state = {
    count : this.props.count
  }
}

2. state更新

假如说我们这个组件中有一个"+"按钮,每次我们按这个按钮的时候,count就需要加1,其更新的方式为:

handleClick () {
  this.setState({
    count : this.state.count + 1
  })
}

一个完整的例子

class MyCount extends Component {
  constructor () {
    super()
    this.state = {
      count : this.props.initNumber || 0
    }
  }

  handleAddClick () {
    this.setState({
      count : this.state.count + 1
    }) 
  }

  render () {
    return (
      <div>
        { this.state.count }
        <button onClick={this.handleAddClick.bind(this)}>+</button>
        <button>-</button>
      </div>
    )
  }
}

相关文章

  • #React-Redux

    React-Redux是Redux的官方React绑定库。它能够使你的React组件从Redux store中读取...

  • React-redux概念学习

    React-redux 概述 react-redux 能够使你的React组件从React store中很方便的读...

  • react-redux中的connect方法解析

    connect()是react-redux中的核心方法之一,它将react组件预redux中的Store真正连接在...

  • 记录react相关一些功能使用(持续更新)

    React-redux connect功能:连接容器组件和展示组件使用 React Redux 库的 connec...

  • Redux——React 组件中的数据传输

    React中的数据有两种:prop和state。其中prop负责对外的数据交互,state负责内部的数据管理。 R...

  • react-redux

    react-redux react-redux可以使redux的state成为组件的属性,dispatch的act...

  • RN:React-Redux

    目录一. React-Redux是什么 1. React-Redux的组件拆分规范  1.1 UI组件  1.2 ...

  • react-router路由守卫

    安装redux和react redux index.js 用react-redux的Provider复合组件实现逐...

  • react-redux

    react-redux 主要: react-redux与redux并不完全一样 主要特征 ui组件 容器组件由 R...

  • react 笔记

    react 基本概念解析 react 的组件声明周期 react 高阶组件,context, redux 等高级...

网友评论

      本文标题:Redux——React 组件中的数据传输

      本文链接:https://www.haomeiwen.com/subject/zcahuqtx.html