0

im trying to render something like whatsapp, in the main screen you got all the groups rendered, and when you click on a group then a new route become active and the main screen disabled, i want that when the user click on the group (its a object of course) then the route that will respond to that event will somehow get the group object of the component that contain the to the group.

ill show you a description of the code:

<Route exact path="/" render={props => (
    <GroupScreen groups={this.state.groups} />
)} />
<Route exact path="/conversation_area" render={props => (
    <h1>bla bla</h1>
)} />

in the groupsScreen component each single group component get a group object that related to her, inside the group representation in the main screen you can click on it and it will route you to /conversation_area, now i know i can pass the name of the group in the url for example and than iterate over this.state.groups and this will work, but i wonder if there is a better way to communicate between link and a route...

the group component is simple, just a component that get a group object in his props, and a that wrap all the design of the group.

thanks alot!

1 Answers1

1

If you have the groups in state, you could filter state to only pass in the value that you want to.

<Route exact path="/conversation_area/:group_id" render={props => {
    // filter out the object that you need from the groups.
    const group = props.find(group => props.match.params.group_id === group.id)

    // you now have access to that group information.
    return (
        <h1>bla bla</h1>
)}} />

I'd restructure the code here and use the component prop to pass in a component. Keep the Routing simple, it'll save you a lot of hassle later on.

Strahinja Ajvaz
  • 1,706
  • 4
  • 15
  • 33