简体   繁体   English

反应更清洁的方式来提升 state

[英]React cleaner way to lift state up

I've read like 5 SO questions and https://reactjs.org/docs/lifting-state-up.html and I haven't found a better way to do this.我已经阅读了 5 个 SO 问题和https://reactjs.org/docs/lifting-state-up.html并且我还没有找到更好的方法来做到这一点。 I need to toggle a pop-up window from somewhere else in my React project, and their lowest ancestor is App.我需要从我的 React 项目的其他地方切换弹出窗口 window,它们的最低祖先是 App。 I need to go through like 20 components across many different files if I want to lift the state up the traditional way (to App, and all the way down to both components).如果我想以传统方式提升 state(到 App,一直到两个组件),我需要 go 通过许多不同文件中的 20 个组件。 The affected hierarchy looks something like this:受影响的层次结构如下所示:

App --> RightColumn --> Search --> ... --> SearchCardEntry --> SearchCardEntryColumn
    --> PopupWrapper --> PopupBook

I think there has to be a better way, especially with bigger projects with thousands of Components.我认为必须有更好的方法,尤其是对于具有数千个组件的大型项目。

You can use React Context to easily share data components wide and avoid moving state among way too many layers.您可以使用React Context轻松地广泛共享数据组件,并避免在太多层之间移动 state。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM