父亲给儿子传递数据 —— Props Down:
//Parent.js function Parent(){ let [age] = useState(20) return <Child count={age}/> } |
//Child.js function Child( props ){ clog( props.count ) } |
儿子给父亲传递数据 —— Props Up:
//Parent.js function Parent(){ function showPage( n ){ clg('父组件的方法想使用子组件的数据:', n) } return <Child f1={showPage}/> //父组件把自己的方法传递给子组件 } |
//Child.js function Child( props ){ let pno = 3 props.f1( pno ) //子组件通过自己的属性调用了父组件的方法,实参就是子组件的数据 } |