美文网首页
react数据

react数据

作者: 夏夏夏夏顿天 | 来源:发表于2018-09-26 14:50 被阅读6次

react数据是存在state里面的,在constructor方法里面

父子组件传值放在super(props)这个props里面

state是组件内的数据,我们先在constructor里面初始化一个数据,然后在render方法里面利用jsx语法渲染处理就行

下面是实例效果:

state数据

  • 获取数据this.state
  • 修改数据this.setState()
class Index extends Components {
   constructor () {
     super(props)  // 这里面的数据来自父组件的数据
     this.state = { data: '我是子组件的私有数据'}
  }
 render () {
     return (
        <div>{data}</div>
     )
  }
}

在jsx语法中需要渲染变量的一律加上花括号就可以了

列表渲染

一般情况下,都是数据是数组,然后里面es6里面的数组方法map方法

....
render () {
     return (
         {
          _arrs.map((item, i) => {
              <div>{item.name}</div>
          })
         }
      )

}

相关文章

网友评论

      本文标题:react数据

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