1

I have this snippet of code that toggles between different components. I would like to pass values down to the different components in an easy way, like this;

<Route path="student" component={Student} websocket={self.websocket}/>

Is there an easy way of doing this? I have looked at cloning classes etc, but I don't think those answers are "good enough" I guess. I haven't found a similar situation and hope someone can answer my question.

The code in "full";

ReactDOM.render(
<Router history={browserHistory}>
  <Route path="/" component={App}>
    <Route path="student" component={Student}/>
    <Route path="about" component={About}/>
    <Route path="login" component={LoginForm}/>
    <Route path="oauth" component={Knapp}/>
  </Route>
</Router>, document.getElementById("container") );

1 Answer 1

1

You can do something like this

class Example extends React.Component { 
  constructor(props) {
    super(props);
  }
  render() {
    return (
      <h1>
        Checking props - {this.props.route.foo}
      </h1>
    );
  }

}

var routes = (
  <Route path="/" foo="bar" component={Example}/>
);

Also you can look here how to do it in another way

I hope it will help you Thanks

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, good suggestion. I can't really make it work like this, not sure why, but every time I pass in an object or anything other than a string, I get "undefined" in the Student-component. I will however, look more into this. Thank you for your answer.
In other words, your answer probably works for other people!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.