简体   繁体   English

并行组件的反应通过状态

[英]React Pass State Across Parallel Components

I have two Components of the same level (no owner-ownee relation). 我有两个相同级别的组件(没有所有者 - 欠者关系)。 Suppose by clicking "C" in Component A, I set the temperature unit to "C", and clicking "F", I set the unit to "F". 假设通过单击组件A中的“C”,我将温度单位设置为“C”,然后单击“F”,我将单位设置为“F”。 In Component B, I need to display the temperature, so I need to get the unit. 在组件B中,我需要显示温度,所以我需要得到单位。 But that information is in A. I wonder how I can access the state in Component A? 但是那个信息在A中。我想知道如何访问组件A中的状态?

Follow up: 跟进:

In another Component C, which is also parallel to A and B, I have a link to another page: 在另一个也与A和B并行的组件C中,我有一个指向另一个页面的链接:

<div className="bottom_link">
     <Link to={'weather-stations/eaB1rJytnpS5mZ2m'}>view detail</Link>
</div>

I also need to pass the unit info to that page. 我还需要将单元信息传递给该页面。 I wonder what is the recommended way to do this? 我想知道推荐的方法是什么? Thanks. 谢谢。

Have all of your components, A, B and C, owned by component X and move the state there. 拥有组件X拥有的所有组件A,B和C,并将状态移动到那里。 Have a handler in component X that you pass into the children, and that handler will do a setState on X. 在组件X中有一个传递给子setState处理程序,该处理程序将在X上执行setState

An alternative is to use the Flux approach. 另一种方法是使用Flux方法。 Here you would use a store and have each component read from that shared store's state. 在这里,您将使用商店并从该共享商店的状态读取每个组件。 The change from F to C and back would be done via an action creator and store handler. 从F到C和后退的更改将通过动作创建者和商店处理程序完成。

The second approach is an extrapolation of the first: you are essentially 'lifting' the state up into the global scope with a store, whereas you are lifting it into X's scope by using X's state. 第二种方法是对第一种方法的推断:您实际上是通过商店将状态“提升”到全局范围,而使用X的状态将其提升到X的范围。

In general in React, you want your components to be as stateless as possible. 通常在React中,您希望组件尽可能无状态。 Therefore I would recommend using the first approach. 因此我建议使用第一种方法。 You can then lift the state higher if necessary (say X, Y and Z need to share the state) and A, B and C are left untouched. 然后,如果需要,您可以将状态提升到更高的位置(例如,X,Y和Z需要共享状态),A,B和C保持不变。

Using props rather than state has the nice side effect of forcing you to think about the API of your components. 使用道具而不是状态会产生很好的副作用,迫使您考虑组件的API。 Exactly what data does this component require to do its job? 这个组件究竟需要做什么数据来完成它的工作? Then those requirements are passed as props. 然后这些要求作为道具传递。

If I were to give any single piece of advice to someone starting in React, it would simply to not use state whenever possible. 如果我要向在React开始的人提供任何单一建议,那么只要不尽可能不使用状态。 Even when you think you need it, you can often remove it, so you should be trying as hard as possible to avoid state. 即使你认为你需要它,你也可以经常删除它,所以你应该尽可能地努力避免状态。

I'm working on an app with hundreds and hundreds of components, and I can barely think of a place where we use this.setState 我正在开发一个包含数百个组件的应用程序,我几乎无法想到我们使用this.setState的地方

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

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