zoukankan      html  css  js  c++  java
  • 菜比如我的漫漫react学习路(二)

    基础部分学差不多了,说说state和props

    state:

    class listData extends React.Component{

      //es6 的 语法,详情请出门右转看ES6

      constructor(...props){
        super(...props)
         }

      //也就这个state这个没有用react自己的getInitialState

      this.state = {

        testState:1

      }

      //其他的react生命周期照样可以使用,不过,注意没有逗号~

      componentDidMount(){

        //code balabala

      }

      changeState(){

        this.setState({

          testState: ++ this.state.testState

        },()=>{//这个是setState的第二个参数,也就是一个function,就是一个回调函数,在state修改完之后,你想做的事情就可以放在这里

          console.log(this.state.testState)

        })

      }

      render(){

        return(

          <div onClick = {this.changeState.bind(this)}>测试那么一下</div>

        )  

      }

    }

    现在来说说props

    除去redux里的,我现在用到的props,都是在父子组件之间传值

    let Parent = React.createClass({

      getInitialState({

        return{

          tabIndex:1

        }

      }),

      render(){

        return(

          <div>

            <Child parent = {this}/>//把需要传给子组件的东西放在这里,可以直接把父组件的this传给子组件

          </div>

        )

      }

    })

    let Child = React.createClass({

      render(){

        return(

          <div>{this.props.parent.state.tabIndex}</div>  //this.props.parent取到的就是父组件传过来的this,父组件的this都拿到了,那么父组件里的其他东西也就可以拿到了。

        )

      }

    })

    当你终于脱胎换骨,一定会感谢曾经的孤独。
  • 相关阅读:
    【转】 C++模板详解
    【转】 memcmp源码实现
    【转】 C++的精髓——虚函数
    【转】 如何使用Valgrind memcheck工具进行C/C++的内存泄漏检测
    【转】 优秀代码所具备的5大品质
    爬取贴吧中的html,并保存到相对应的文件夹中
    urllib模块通过post请求获取数据
    django,uwsgi, nginx部署项目
    Django中Ajax处理
    Django中的session于cookie的用法
  • 原文地址:https://www.cnblogs.com/zdzx939/p/6762113.html
Copyright © 2011-2022 走看看