本文主要和大家介绍react.js 父子组件数据绑定实时通讯的示例代码,react.js我自己还在摸索学习中,碰到父子组件数据绑定实时通讯的问题,研究了一下,分享给大家,也给自己留个笔记:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | import React,{Component} from 'react'
import ReactDOM from 'react-dom'
class ChildCounter extends Component{
render(){
return (
<p style={{border: '1px solid red' }}>
{ this .props.count}
</p>
)
}
}
class Counter extends Component{
static defaultProps={
number:5
}
constructor(props){
super (props);
this .state={
number:props.number
}
}
componentWillMount(){
console.log( '组件将要挂载' )
}
componentDidMount(){
console.log( "组件挂载完成" )
}
handleClick=()=>{
this .setState((prev,next)=>({
number:prev.number+1
}),()=>{
console.log( "回调函数执行" )
})
}
render(){
return (
<p>
<p>{ this .state.number}</p>
<button onClick={ this .handleClick}>+</button>
<ChildCounter count={ this .state.number}></ChildCounter>
</p>
)
}
}
ReactDOM.render(<Counter></Counter>,document.querySelector( "#root" ))
|
登录后复制
相关推荐:
Vue2.0父子组件传递函数详解
vue中的event bus非父子组件通信详解
React高阶组件实例解析
以上就是实例详解react.js父子组件数据绑定实时通讯的详细内容,更多请关注php中文网其它相关文章!