简体   繁体   English

如何更新代表相同数据的两个不同的React组件实例?

[英]How to update two different React component instances that represent the same data?

Say I've defined a <Tag> component that has two props: id and name . 说我已经定义了一个具有两个道具的<Tag>组件: idname My application renders this component in two different places, so I have a <Tag> component whose id is 1 appearing twice, but whose owners are different. 我的应用程序在两个不同的位置渲染此组件,因此我有一个<Tag>组件,其ID为1出现了两次,但所有者不同。 One tag is owned by an <Entries> component, and the other is owned by a <Tags> component. 一个标签由<Entries>组件拥有,另一个<Tags><Tags>组件拥有。

When a <Tag> is clicked, I want to change its state, and I want all other <Tag> components with the same id to also change their state so that they match. 单击<Tag> ,我想更改其状态,并且希望所有其他具有相同id <Tag>组件也更改其状态,以便它们匹配。 What's the best way to do this? 最好的方法是什么? Should all <Tags> have the same root component? 所有<Tags>是否应具有相同的根组件? I don't like that idea, because I feel like it's a brittle solution. 我不喜欢这个主意,因为我觉得这是一个脆弱的解决方案。

I think you have two options to go: 我认为您有两种选择:

1- React way 1-反应方式

  • You should keep the state in a common owner component of <Tags> and <Entries> . 您应将状态保留在<Tags><Entries>的公共所有者组件中。
  • You should pass a change handler from the state owner component to the <Tag> component 您应该将变更处理程序从状态所有者组件传递到<Tag>组件
  • When the tag changes the state owner will update <Tag> s in both <Tags> and <Entries> 标签更改时,状态所有者将同时更新<Tags><Entries> <Tag>

  • Reading this should give you a lot of insight: http://facebook.github.io/react/docs/thinking-in-react.html 阅读此书应该会给您带来很多见识: http : //facebook.github.io/react/docs/thinking-in-react.html

2- Pure Flux way 2-纯助焊剂方式

  • If your application is a little more complex than what you have told you can go with flux architecture. 如果您的应用程序比您所说的复杂得多,则可以使用助焊剂体系结构。
  • To do this you should have actions,dispatchers and stores implemented in your application. 为此,您应该在应用程序中实现操作,调度程序和存储。
  • When a <Tag> is changed you call an action, which in turn calls a dispatcher, which is in turn listened by stores, which in turn emits a change event to components. 更改<Tag>后,您将调用一个操作,该操作又调用一个调度程序,而调度程序又被商店侦听,该调度程序又向组件发出一个change事件。 Then all the components updates themselves with the new state. 然后,所有组件都将自己更新为新状态。

  • You can find about flux here: http://facebook.github.io/flux/docs/overview.html 您可以在此处找到有关通量的信息: http : //facebook.github.io/flux/docs/overview.html

id's should generally be unique within an html document. id在html文档中通常应该是唯一的。 The best solution would be to change your id's to classes. 最好的解决方案是将您的ID更改为类。

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

相关问题 React:componentDidUpdate对于组件的2个不同实例是否相同? - React: is componentDidUpdate same for 2 different instances of a component? 在同一个 React 组件 React 的两个特定实例之间共享 State - Share State between two specific instances of the same react component React 如果两个不同的依赖 arrays 同时更新,react useEffect 如何更新数据 - react useEffect how to update data if two different dependency arrays updates at the same time 在同一个React组件的不同实例之间切换时使用componentDidMount - Utilizing componentDidMount when switching between different instances of the same React component 创建 React 组件的不同实例 - Creating different instances of a React component 如何在两个不同的.js文件中使用相同的数据(React Native) - How to use same data in two different .js file (React Native) 如何在不同的ReactJS组件实例中拥有不同的数据? - How can I have different data in different ReactJS Component instances? 如何隔离同一组件的不同实例之间的js变量? - How to isolate js variables between different instances of the same component? 如何在同一页面上的多个实例上使用React组件? - How can I use a React component on multiple instances on the same page? React 在组件中为相同的 prop 显示两个不同的值 - React shows two different value for same prop in component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM