Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

60. React组件通信如何实现? #60

Open
wqjiao opened this issue May 14, 2020 · 0 comments
Open

60. React组件通信如何实现? #60

wqjiao opened this issue May 14, 2020 · 0 comments

Comments

@wqjiao
Copy link
Owner

wqjiao commented May 14, 2020

  1. 父组件向子组件通讯: 父组件可以向子组件通过传 props 的方式,向子组件进行通讯;

2.子组件向父组件通讯: props+回调的方式,父组件向子组件传递props进行通讯,此props为作用域为父组件自身的函数,子组件调用该函数,将子组件想要传递的信息,作为参数,传递到父组件的作用域中;

3.兄弟组件通信: 找到这两个兄弟节点共同的父节点,结合上面两种方式由父节点转发信息进行通信
跨层级通信: Context设计目的是为了共享那些对于一个组件树而言是“全局”的数据,例如当前认证的用户、主题或首选语言,�对于跨越多层的全局数据通过Context通信再适合不过

  1. 发布订阅模式: 发布者发布事件,订阅者监听事件并做出反应,我们可以通过引入event模块进行通信

  2. 全局状态管理工具: 借助Redux或者Mobx等全局状态管理工具进行通信,这种工具会维护一个全局状态中心Store,并根据不同的事件产生新的状态
    参考2019年17道高频React面试题及详解

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant