6

enter image description here

How can I implement dynamic paths when I click on a link like in a picture?

The user clicks on the link and goes to the message section, then clicks on the link (Link) and goes to the desired component.

DATA:

const data = [
    {
        link: 'Link Name 1',
        text: 'Text 1',
        id: '1'
    },
    {
        link: 'Link Name 2',
        text: 'Text 2',
        id: '2'
    },
    {
        link: 'Link Name 3',
        text: 'Text 3',
        id: '3'
    }
    ];

App.js

<Switch>
 <Route exact path="/" component={AppHome}/>
 <Route exact path="/messages" component={AppMessages}/>
 <Route exact path="/messages/chat" component={AppMessageItems}/>
 <Route exact path="/messages/chat/:id" component={AppMessageItems}/>
</Switch>

Messages.js

const AppMessages = () => {
             <ul>
                {data.map(function(item, index) {
                    return (
                        <li key={index}>
                            <Link to='/messages/chat'><AppMessageUserList {...item} /></Link>
                        </li>);
                })}
            </ul>
}

AppMessageUserList

const AppMessageUserList = ( props ) => {
const {link} = props;
   return ( <div> {link} </div> );

}

AppMessageItems.js

const AppMessageItems = () => {
   return ( <h1> Hello World </h1> );

}

But when i try this code i get error when click to link.

6
  • try to pass the id on AppMessageItems that will try to map in the exact page what you are looking for Commented Feb 19, 2020 at 8:16
  • what is the error ? Commented Feb 19, 2020 at 8:18
  • @ionMobDev can you write code how i can do it ? Commented Feb 19, 2020 at 8:19
  • and whats in AppMessageUserList Commented Feb 19, 2020 at 8:20
  • @ZunaibImtiaz edit my post and add AppMessageUserList Commented Feb 19, 2020 at 8:22

1 Answer 1

3

Replace your code with

// App.js
<Switch>
 <Route exact path="/" component={AppHome}/>
 <Route exact path="/messages" component={AppMessages}/>
 <Route path={["/messages/chat", "/messages/chat/:id"]} component={AppMessageItems}/>
</Switch>

// Messages.js
// Assuming item has id and upon clicking the item should change the url

const AppMessages = () => (
  <ul>
    {data.map((item, index) => 
      (
        <li key={item.id}>
          <Link to={`/messages/chat/${item.id}`}>
             <AppMessageUserList {...item} />
           </Link>
         </li>
       )
     }
    </ul>
)


// AppMessageItems

const AppMessageItems = (props) => {
if (props.match.params && props.match.params.id) {
   // return the selected items
 }
// return whatever you wanted to return if nothing is selected 
}

NOTE: Don't use index as key

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

9 Comments

Thanks for code, but when i use <Route path=["/messages/chat", "/messages/chat/:id"] component={AppMessageItems}/> terminal show error "Tag start is not closed"
you misplaced [, it should be <Route path=["/messages/chat", "/messages/chat/:id"] component={AppMessageItems}/>
Yes, i'm using this way <Route path=["/messages/chat", "/messages/chat/:id"] component={AppMessageItems}/> same with example, but "Tag start is not closed"
I try path={["/messages/chat", "/messages/chat/:id"]} and works
Yea, I missed curly braces {} 😅
|

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.