简体   繁体   English

在其他React组件上传递道具

[英]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组合中做出反应,这是非常简化的工作流程。

  • All components get data for display from state stored in redux store. 所有组件都从redux存储中存储的状态获取数据以进行显示。
  • Navigation component would dispatch actions to the redux store when something is clicked in them or changed somehow. 导航组件会在单击某些操作或对其进行某种更改时将操作分派到redux存储。
  • Reducers in redux store will recognize those actions and make changes to the state in the redux store Redux存储中的reducer将识别这些操作并更改Redux存储中的状态
  • Once changes are done, all components gets rendered afresh using new data from redux store 更改完成后,将使用redux存储中的新数据重新渲染所有组件

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

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