react的特点:
1.对比全统的JQ,它的数据流是单向的,controller层到Model层到view层。
2.组件化, react框架只操作组件,每个组件需要从react继承过来。也就是说组件的定义需要安装react的规则来。组件中定义组件的MVC三种状态。
3.JSX,react的新语法,它是html+js的混合体,每个react组件都必须实现它的render方法,并返回一个JSX。JSX内容比较多,需要是参考即可
4.render方法的特点。必须返回有根目录的JSX, 同时每次组件的state变化时,被调用。
react的生命周期:
加载先执行constructor
componentDidMount,(装载完成),在render之后调用。
父子组件的传值:
父传递子,通过标签赋值来传递。子传父有两种方法来传递,一是子组件的值传给父。在父自己定义方法后通过标签传递给子组件,在子组件通过props调用,
注意这里子组件中调用父组件方法的第一个参数是this。二是子组件的方法传递给父自己,这需要把子组件传递给父组件,在父组件标签中添加<Children ref="children" />
然后在父组件调用子组件的值:
getChildrenMsg = () => { this.setState({ childrenMsg: this.refs['children'].state.msg }) }
useEffect的使用
1.react中函数是没有生命周期的, useEffect
Hook 看做 componentDidMount
,componentDidUpdate
和 componentWillUnmount
这三个函数的组合。useEffect的第二个参数为数组,
如果useEffect执行依赖state或者props的成员,需要在数组添加该成员,否则不用添加。