ReactJS: Pass parameter from rails to react router to component
Asked Answered
B

3

10

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

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

Routes.jsx

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

App.jsx (component)

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 complete flow.

I have tried the following:

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

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

Braxton answered 14/11, 2017 at 22:10 Comment(6)
Use render insteadBountiful
Which version of React Router are you using?Bedrock
@Bedrock I am using react-router 3.0.5Braxton
@SterlingArcher could you provide an end to end answer using render? That would greatly helpBraxton
@AlexJose sure, here's an example of how I use it with props github.com/RUJodan/Source-React/blob/master/src/index.jsxBountiful
@SterlingArcher Thanks for the example, but I am not sure if that answers my question. I am looking on how I can pass a parameter from the "view" to the "react router" to the "component".Braxton
C
1

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

We will start from the view:

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

Now we will create a component 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" component={Login} />
      </Switch>
    )
  }
}

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

class App extends Component{
  constructor(props){
    super(props)
  }
  render(){
    return(
      <h1>{this.props.test}</h1>
    )
  }
}
Chantress answered 19/11, 2017 at 3:24 Comment(2)
This seems to throw an error at the first step itself, while calling the component from the view. ArgumentError(wrong number of arguments (given 3, expected 1..2)Braxton
try removing prerender: falseChantress
D
1
<Route path="/" render={attr => <App {...attr} test="abc" />} />
Dorty answered 23/11, 2017 at 13:6 Comment(0)
F
1

In Router v3 you would do something like this

Wrap your App component 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="/" component={APP}>
  <Route path="/lobby" component={Lobby} />
  <Route path="/map" component={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!

Fetishism answered 23/11, 2017 at 20:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.