如何在ReactJS中的子组件(兄弟姐妹)之间共享状态? [英] How to share state between child component (siblings) in ReactJS?

查看:154
本文介绍了如何在ReactJS中的子组件(兄弟姐妹)之间共享状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将状态传递给兄弟姐妹甚至是祖父母。

I would like to pass state to a sibling or even a grandparent whatever.

我有3个组件。在 Header 里面,我有一个带onClick功能的按钮,用于切换 Navigation 里面的下拉菜单。顺便说一句,我想将相同的状态传递给 AnotherComponent

I have 3 components. Inside Header, I have a button with an onClick function to toggle a Dropdown Menu inside Navigation. And by the way, I would like to pass the same state to AnotherComponent.

如何传递状态(如as isDropdownOpened )从标题导航 AnotherComponent

How to pass state (such as isDropdownOpened) from Header to Navigation and AnotherComponent?

<div>
     <Header />
       <Navigation />
        <div>
          <div>
             <div>
               <AnotherComponent />
             </div>
           </div>
        </div>
</div>


推荐答案

这篇关于如何在ReactJS中的子组件(兄弟姐妹)之间共享状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆