如何在路由中将状态从父组件传递到子组件(react-route-dom)reactjs [英] How to pass state from parent Component to child component in route (react-route-dom) reactjs

查看:75
本文介绍了如何在路由中将状态从父组件传递到子组件(react-route-dom)reactjs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 React.js 的新手,并使用 react-router-dom 创建了一个小型 React 应用程序.其中我有两个组件:

I am new to React.js and created a small react application using react-router-dom. In which I have two components:

  1. 仪表板(dashboard.js)
  2. 信息(information.js)

和一个主要的组件应用程序,App.js,我在其中使用了 react-router-dom.

and one main Component App, App.js, in which I am using the react-router-dom.

<Route exact path="/dashboard" render={props => <Dashboard someProp="2" {...props} />} />
<Route exact path="/information" render={props => <Information someProp="2" {...props} />} />

我能够将道具从 App 组件发送到仪表板和信息,但我想发送状态.有人可以帮助我,如何将状态从父组件发送到子组件?

I am able to send props from App component to dashboard and information but I want to send state. Can somebody help me, How can I send state from Parent component to child component?

推荐答案

父组件中可以发送这样的道具

In parent component you can send props like this

<child prop1 = {this.state.stateName} />

这篇关于如何在路由中将状态从父组件传递到子组件(react-route-dom)reactjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆