[英]Passing props on other React Component
I would like passing props to React Component (no parently and no descently). 我想将道具传递给React Component(没有父母也没有血统)。
Example : 范例:
<aside>
<Navigation />
</aside>
<main>
<Video />
</main>
onClick on <a>
element on my Navigation
, i want to update my Video
component state (src). 上的onClick
<a>
我的元素Navigation
,我想更新我的Video
组件的状态(SRC)。
I know i can change state for Child component, but for this case .. :/ 我知道我可以更改Child组件的状态,但是在这种情况下..:/
Anyone can help me ? 有人可以帮助我吗?
Thank you community ! 谢谢社区!
Pass a click handler to Navigation which calls setState
on the parent component (containing both the Navigation and the Video). 将单击处理程序传递给Navigation,它在父组件(包含Navigation和Video)上调用
setState
。 The thing you want to change should be a prop on Video. 您要更改的内容应该是视频的道具。 The setState will cause the Video to be rerendered with the new prop.
setState将导致使用新道具重新渲染视频。
You can set state like this 您可以这样设置状态
1 create a function to set states in your parent component. 1创建一个函数来设置父组件中的状态。
2 pas it like a prop to <Navigation />
2秒钟,就像
<Navigation />
的道具一样
3 use this prop onclick in your <Navigation />
3在
<Navigation />
使用此道具onclick
One way to do it is to use redux together with react. 一种方法是将redux与react一起使用。 While redux is quite large step to learn, once you master it itnwill help you solve problems like this easily.
虽然redux是学习中相当大的一步,但是一旦掌握它,它将帮助您轻松解决此类问题。
To give you very rough idea how would things work in react with redux combination, here is very simplified workflow. 为了让您很粗略地了解如何在Redux组合中做出反应,这是非常简化的工作流程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.