最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ReactJS: Pass parameter from rails to react router to component - Stack Overflow

programmeradmin1浏览0评论

I am trying to pass a value from the render function to the ponent:

= react_ponent('App', props: {test: 'abc'}, prerender: false)

Routes.jsx

<Route path="/" ponent={App} >

App.jsx (ponent)

class App extends React.Component {
  render() {
    return (
      <Header test={this.props.test}>
      </Header>
      {this.props.children}
      <Footer />
    );
  }
}

App.propTypes = { test: PropTypes.string };

There does not seem to be a coherent answer to this plete flow.

I have tried the following:

<Route path="/" ponent={() => (<App myProp="value" />)}/>

But this still does not answer the question of picking up the value provided by the initial render call(react_ponent)

I am trying to pass a value from the render function to the ponent:

= react_ponent('App', props: {test: 'abc'}, prerender: false)

Routes.jsx

<Route path="/" ponent={App} >

App.jsx (ponent)

class App extends React.Component {
  render() {
    return (
      <Header test={this.props.test}>
      </Header>
      {this.props.children}
      <Footer />
    );
  }
}

App.propTypes = { test: PropTypes.string };

There does not seem to be a coherent answer to this plete flow.

I have tried the following:

<Route path="/" ponent={() => (<App myProp="value" />)}/>

But this still does not answer the question of picking up the value provided by the initial render call(react_ponent)

Share Improve this question asked Nov 14, 2017 at 22:10 Alex JoseAlex Jose 2783 silver badges17 bronze badges 6
  • Use render instead – Sterling Archer Commented Nov 14, 2017 at 22:12
  • Which version of React Router are you using? – Dez Commented Nov 14, 2017 at 22:18
  • @Dez I am using react-router 3.0.5 – Alex Jose Commented Nov 15, 2017 at 0:59
  • @SterlingArcher could you provide an end to end answer using render? That would greatly help – Alex Jose Commented Nov 15, 2017 at 0:59
  • @AlexJose sure, here's an example of how I use it with props github./RUJodan/Source-React/blob/master/src/index.jsx – Sterling Archer Commented Nov 15, 2017 at 15:12
 |  Show 1 more ment

3 Answers 3

Reset to default 1

Looking for an end to end answer on how to pass a parameter from the "view" to the "react router" to the "ponent"

We will start from the view:

<%= react_ponent('MyRoute', {test: 123}, prerender: false) %>

Now we will create a ponent that holds our route:

class MyRoute extends Component{
  constructor(props){
    super(props)
  }
  render(){
    return(
      <Switch>
        <Route path="/" render={() => <App test={this.props.test} />} />
        <Route path="/login" ponent={Login} />
      </Switch>
    )
  }
}

As you can see, we passed the test prop from the Route ponent to the App ponent. Now we can use the test prop in the App ponent:

class App extends Component{
  constructor(props){
    super(props)
  }
  render(){
    return(
      <h1>{this.props.test}</h1>
    )
  }
}
<Route path="/" render={attr => <App {...attr} test="abc" />} />

In Router v3 you would do something like this

Wrap your App ponent under withRouter like this

import { withRouter } from 'react-router';

class App extends React.Component {
  render() {
    return (
      <Header test={this.props.test}>
      </Header>
      {
        this.props.children &&
        React.clone(this.props.children, {...this.props} )}
      <Footer />
    );
  }
}
App.propTypes = { test: PropTypes.string };
export const APP = withRouter(App);

And construct your routes like this...

<Route path="/" ponent={APP}>
  <Route path="/lobby" ponent={Lobby} />
  <Route path="/map" ponent={GameMap} />
  ...
</Route>

So your child routes will be rendered inside the APP children property an the props will be passed down to them.

Hope this helps!

发布评论

评论列表(0)

  1. 暂无评论