React 组件通信 02
跨级组件通信
跨级组件通信就是父组件与子组件的子组件或者更深层次的通信。我们可以让父组件传给子组件,再让子组件传递给他的子组件,一次逐级传递。但是这种传递方式当层级关系超过两层的时候会过于臃肿,不好维护。Contex 通过组件树提供了一个传递数据的方法,从而避免在每个层级之间手动传递 props 属性。
祖先代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import React from "react"; import PropTypes from "prop-types"; import Sub from "./Sub.js"; export default class App extends React.Component { static childContextTypes = { color: PropTypes.string, callback: PropTypes.func, }; getChildContext() { return { color: "pink", callback: this.callback.bind(this), }; } callback(msg) { alert(msg); } render() { return <Sub />; } }
|
中间组件代码示例:
1 2 3 4 5
| import React from "react"; import SubSub from "./SubSub.js"; export default function Sub(props) { return <SubSub />; }
|
后代组件代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import React from "react"; import PropTypes from "prop-types"; export default class SubSub extends React.Component { static contextTypes = { color: PropTypes.string, callback: PropTypes.func, }; render() { const style = { color: this.context.color }; const cb = (msg) => { return () => { this.context.callback(msg); }; }; return ( <div style={style}> <h1>In SubSub.js</h1> // 粉色字体展示 In SubSub.js <button onClick={cb("this is SubSub.js")}>click me</button> this is SubSub.js 字样 </div> ); } }
|
以上可以看出,祖先组件声明了 context 上下文,后代组件通过 this.context 获取祖先组件传递下来的内容
注意:如果后代组件使用构造函数 Constructor,那么 context 需要作为构造函数的第二个参数传入,否则无法使用
1 2 3 4
| constructor (props, context) { super(props) ... }
|