learn
learn copied to clipboard
React 组件之间如何通信
React 组件通信如何实现
父组件向子组件通讯
父组件可以向子组件通过传 props 的方式,向子组件进行通讯。
import React from "react";
class Son extends React.Component {
render() {
return <p>{this.props.text}</p>;
}
}
class Father extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<Son text="这是父组件传给子组件的内容" />
</div>
);
}
}
子组件向父组件通讯
props + 回调的方式,父组件向子组件传递 props 进行通讯,此 props 为作用域为父组件自身的函数,子组件调用该函数,将子组件想要传递的信息,作为参数,传递到父组件的作用域中
import React from "react";
class Son extends React.Component {
render() {
return <p onClick={this.props.onClick}>{this.props.text}</p>;
}
}
class Father extends React.Component {
constructor(props) {
super(props);
this.state = {
fatherToSonText: "父组件传给子组件的内容",
sonToFatherText: "子组件传给父组件的内容"
};
}
handleClick(text) {
alert(text);
}
render() {
return (
<Son
text={this.state.fatherToSonText}
onClick={e => this.handleClick(this.state.sonToFatherText, e)}
/>
);
}
}
兄弟组件通信
找到这两个兄弟节点共同的父节点,结合上面两种方式由父节点转发信息进行通信
import React from "react";
class FirstSon extends React.Component {
render() {
return <h2 onClick={this.props.onClick}>戳我,我要让旁边那位变成红色</h2>;
}
}
class SecondSon extends React.Component {
render() {
return <h2 style={{ color: this.props.color }}>我是你旁边那位</h2>;
}
}
class Father extends React.Component {
constructor(props) {
super(props);
this.state = {
color: "#666"
};
}
handleClick() {
this.setState({
color: "red"
});
}
render() {
return (
<div>
<FirstSon onClick={() => this.handleClick()} />
<SecondSon color={this.state.color} />
</div>
);
}
}
跨层级通信 Context
Context 设计目的是为了共享那些对于一个组件树而言是“全局”的数据,例如当前认证的用户、主题或首选语言, 对于跨越多层的全局数据通过 Context 通信再适合不过
import React from "react";
const MyContext = React.createContext(defaultValue);
Context 提供了一个无需为每层组件手动添加 props,就能在组件树间进行数据传递的方法。使用 context, 我们可以避免通过中间元素传递 props:
// Context 可以让我们无须明确地传遍每一个组件,就能将值深入传递进组件树。
const ThemeContext = React.createContext("light");
class App extends React.Component {
render() {
// 使用一个 Provider 来将当前的 theme 传递给以下的组件树。
// 无论多深,任何组件都能读取这个值。
return (
<ThemeContext.Provider value="dark">
<Toolbar />
</ThemeContext.Provider>
);
}
}
// 中间的组件再也不必指明往下传递 theme 了。
function Toolbar() {
return (
<div>
<ThemedButton />
</div>
);
}
class ThemedButton extends React.Component {
// 指定 contextType 读取当前的 theme context。
// React 会往上找到最近的 theme Provider,然后使用它的值。
static contextType = ThemeContext;
render() {
return <Button theme={this.context} />;
}
}
发布订阅模式
发布者发布事件,订阅者监听事件并做出反应,我们可以通过引入 event 模块进行通信
全局状态管理工具
借助 Redux 或者 Mobx 等全局状态管理工具进行通信,这种工具会维护一个全局状态中心 Store,并根据不同的事件产生新的状态