React中组件通信的几种方式
需要组件之进行通信的几种情况
1. 父组件向子组件通信React数据流动是单向的,父组件向子组件通信也是最常见的;父组件通过props向子组件传递需要的信息 import React from 'react'; import PropTypes from 'prop-types'; export default function Child({ name }) { return <h1>Hello,{name}</h1>; } Child.propTypes = { name: PropTypes.string.isRequired,}; Parent.jsx import React,{ Component } from 'react'; import Child from './Child'; class Parent extends Component { render() { return ( <div> <Child name="Sara" /> </div> ); } } export default Parent; 2. 子组件向父组件通信
回调函数实现在子组件中点击隐藏组件按钮可以将自身隐藏的功能 List3.jsx import React,{ Component } from 'react'; import PropTypes from 'prop-types'; class List3 extends Component { static propTypes = { hideConponent: PropTypes.func.isRequired,} render() { return ( <div> 哈哈,我是List3 <button onClick={this.props.hideConponent}>隐藏List3组件</button> </div> ); } } export default List3; App.jsx import React,{ Component } from 'react'; import List3 from './components/List3'; export default class App extends Component { constructor(...args) { super(...args); this.state = { isShowList3: false,}; } showConponent = () => { this.setState({ isShowList3: true,}); } hideConponent = () => { this.setState({ isShowList3: false,}); } render() { return ( <div> <button onClick={this.showConponent}>显示Lists组件</button> { this.state.isShowList3 ? <List3 hideConponent={this.hideConponent} /> : null } </div> ); } } 观察一下实现方法,可以发现它与传统回调函数的实现方法一样.而且setState一般与回调函数均会成对出现,因为回调函数即是转换内部状态是的函数传统; 3. 跨级组件通信
使用context下面例子中的组件关系: ListItem是List的子组件,List是app的子组件 ListItem.jsx import React,{ Component } from 'react'; import PropTypes from 'prop-types'; class ListItem extends Component { // 子组件声明自己要使用context static contextTypes = { color: PropTypes.string,} static propTypes = { value: PropTypes.string,} render() { const { value } = this.props; return ( <li style={{ background: this.context.color }}> <span>{value}</span> </li> ); } } export default ListItem; List.jsx import ListItem from './ListItem'; class List extends Component { // 父组件声明自己支持context static childContextTypes = { color: PropTypes.string,} static propTypes = { list: PropTypes.array,} // 提供一个函数,用来返回相应的context对象 getChildContext() { return { color: 'red',}; } render() { const { list } = this.props; return ( <div> <ul> { list.map((entry,index) => <ListItem key={`list-${index}`} value={entry.text} />,) } </ul> </div> ); } } export default List; App.jsx import React,{ Component } from 'react'; import List from './components/List'; const list = [ { text: '题目一',},{ text: '题目二',]; export default class App extends Component { render() { return ( <div> <List list={list} /> </div> ); } } 4. 没有嵌套关系的组件通信
使用自定义事件的方式下面例子中的组件关系: List1和List2没有任何嵌套关系,App是他们的父组件; 实现这样一个功能: 点击List2中的一个按钮,改变List1中的信息显示 npm install events --save 在src下新建一个util目录里面建一个events.js import { EventEmitter } from 'events'; export default new EventEmitter(); list1.jsx import React,{ Component } from 'react'; import emitter from '../util/events'; class List extends Component { constructor(props) { super(props); this.state = { message: 'List1',}; } componentDidMount() { // 组件装载完成以后声明一个自定义事件 this.eventEmitter = emitter.addListener('changeMessage',(message) => { this.setState({ message,}); }); } componentWillUnmount() { emitter.removeListener(this.eventEmitter); } render() { return ( <div> {this.state.message} </div> ); } } export default List; List2.jsx import React,{ Component } from 'react'; import emitter from '../util/events'; class List2 extends Component { handleClick = (message) => { emitter.emit('changeMessage',message); }; render() { return ( <div> <button onClick={this.handleClick.bind(this,'List2')}>点击我改变List1组件中显示信息</button> </div> ); } } APP.jsx import React,{ Component } from 'react'; import List1 from './components/List1'; import List2 from './components/List2'; export default class App extends Component { render() { return ( <div> <List1 /> <List2 /> </div> ); } } 自定义事件是典型的发布订阅模式,通过向事件对象上添加监听器和触发事件来实现组件之间的通信 总结
参考reactjs官方文档 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |