有没有办法在不使用道具的情况下更新反应组件? [英] Is there a way to update react component without using props?

查看:40
本文介绍了有没有办法在不使用道具的情况下更新反应组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从另一个未连接的组件通过道具更新彼此的反应组件.我该怎么办?

I want to update a react component from another component not connected to each other via props. How can i?

推荐答案

您必须在高于两个组件级别的某处使用状态.然后你必须有一个方法来改变那个状态,然后通过 props 向下传递数据.

You'd have to use state somewhere above the level of both of the components. Then you have to have a method to change that state which then passes the data down via props.

因此,您需要通过钩子上下文或其他一些状态管理(如 redux、mobx 等)进行某种状态管理.为了快速和肮脏,请使用 useContext.

So, you need to have some sort of state management, either by hooks context or some other state management, like redux, mobx, etc. For quick and dirty, use useContext.

这篇关于有没有办法在不使用道具的情况下更新反应组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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