简体   繁体   English

从父级到子级路由的传递状态

[英]Passing state from parent to child route

I am new to react and I am trying to send data from my parent component "Dashboard" to a child component "AddData". 我是新来的响应者,我正在尝试将数据从父组件“仪表板”发送到子组件“ AddData”。 I know the syntax to do this normally, but what is the syntax to do this when a child is connected to parent through Route? 我知道正常执行此操作的语法,但是当孩子通过Route连接到父级时,执行此操作的语法是什么?

Shown below is my Dashboard.jsx: 下面显示的是我的Dashboard.jsx:

import {withStyles} from '@material-ui/core'
import CssBaseline  from '@material-ui/core/CssBaseline'
import PropTypes    from 'prop-types'
import * as React   from 'react'
import {Route}      from 'react-router-dom'
import {Home}       from './scenes/home'
import {A}        from './scenes/a'
import {B}        from './scenes/b'
import {AddData}   from '../adddata'
import {VisualizeData} from '../visualizedata'
import {Header}     from './components/header'
import {Sidebar}    from './components/sidebar'
import styles       from './Dashboard.styles'

class Dashboard extends React.Component {

  constructor() {
    super();

    this.state = {
      trainPercentageDashboard : "null",
      featuresDashboard : [],
      labelsDashboard :  [],
    }
  }

  render() {
    const {classes} = this.props
    console.log(window.location.href);
    var url_ = new URL(window.location.href);
    console.log(url_);
    return (

        <div className={classes.root}>
          <CssBaseline/>
          {url_.pathname !== '/neuralnet' &&
              <Header/>
          }
          <Sidebar/>
          <main className={classes.content}>
            {url_.pathname !== '/neuralnet' &&
                <div className={classes.toolbar}/>
            }
            <Route path="/adddata" component={AddData}/>
            <Route exact path="/visualize" component={VisualizeData} />
            <Route exact path='/home' component={Home}/>
            <Route exact path='/neuralnet' component={A}/>
            <Route exact path='/b' component={B}/>
            <Route exact path='/' component={B}/>
          </main>
        </div>
    )
  }

}

Dashboard.propTypes = {
  classes: PropTypes.object.isRequired,
  theme  : PropTypes.object.isRequired,
}

export default withStyles(styles, {withTheme: true})(Dashboard)

I went through this , but this was not mentioned in it. 我经历了此过程 ,但未在其中提及。

Try this: 尝试这个:

<Route
  path='/adddata'
  render={(props) => <AddData {...props} someotherprop={someotherprop} />}
/>

Note that the passed prop can be a part of the state as well. 请注意,传递的道具也可以是状态的一部分。 So you can pass the state as this.state.someotherprop . 因此,您可以将状态传递为this.state.someotherprop

If you want to pass data down to a component using react router, you can change your Route component to look like this. 如果您想使用React Router将数据向下传递到组件,则可以将Route组件更改为如下所示。

<Route
  path="/adddata" 
  render={(props) => (<AddData {...props}>)}
/>

This is a good article on explaining why to use render rather than component but the gist of it is that it is for performance. 这是一篇很好的文章 ,解释了为什么使用render而不是component但是要点在于它是为了提高性能。

You can pass data from parent component using react-router, is very simple update your code with this, and your issue will be fixed 您可以使用react-router从父组件传递数据,非常简单的方法是使用此命令更新代码,此问题将得到解决

import {withStyles} from '@material-ui/core'
import CssBaseline  from '@material-ui/core/CssBaseline'
import PropTypes    from 'prop-types'
import * as React   from 'react'
import {Route}      from 'react-router-dom'
import {Home}       from './scenes/home'
import {A}        from './scenes/a'
import {B}        from './scenes/b'
import {AddData}   from '../adddata'
import {VisualizeData} from '../visualizedata'
import {Header}     from './components/header'
import {Sidebar}    from './components/sidebar'
import styles       from './Dashboard.styles'

class Dashboard extends React.Component {

  constructor() {
    super();

    this.state = {
      trainPercentageDashboard : "null",
      featuresDashboard : [],
      labelsDashboard :  [],
      name:'rizwan',
    }
  }

  render() {
    const {classes} = this.props
    console.log(window.location.href);
    var url_ = new URL(window.location.href);
    console.log(url_);
    return (

        <div className={classes.root}>
          <CssBaseline/>
          {url_.pathname !== '/neuralnet' &&
              <Header/>
          }
          <Sidebar/>
          <main className={classes.content}>
            {url_.pathname !== '/neuralnet' &&
                <div className={classes.toolbar}/>
            }
            <Route 
                 path="/adddata"
                 component={AddData} 
                 currentStep={this.state.name} />
            <Route exact path="/visualize" component={VisualizeData} />
            <Route exact path='/home' component={Home}/>
            <Route exact path='/neuralnet' component={A}/>
            <Route exact path='/b' component={B}/>
            <Route exact path='/' component={B}/>
          </main>
        </div>
    )
  }

}

Dashboard.propTypes = {
  classes: PropTypes.object.isRequired,
  theme  : PropTypes.object.isRequired,
}

export default withStyles(styles, {withTheme: true})(Dashboard)

Now open your child component: "adddata" 现在打开您的子组件:“ adddata”

and console props 和控制台道具

console.log('wao', this.porps)

thanks, 谢谢,

like and vote up 喜欢并投票

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

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