Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React render link containing a variable

I am trying to do the following:

<tbody>
    {projects.map(function(project, i) { return (
    <tr key={i}>
        <td>
            {project.id}
        </td>
        <td>
            <a href='#/systemlist?projId={project.id}'>{project.name}</a>
        </td>
    </tr>
    );} )}
</tbody>

It should point to another component, using router. It all works, except the {project.id} not being rendered as a value. How to do that? Everything else outside of href works.

like image 756
Kokesh Avatar asked Jan 29 '16 13:01

Kokesh


People also ask

Can I use a href In React?

This href attribute contains the URL or path to the destination page. It may be a relative URL or an absolute URL. In React, relative URLs should always be handled by the link tag provided by the React Router, and pure anchor tags should only be used for absolute paths.

How do I embed a link in React?

To add the link in the menu, use the <NavLink /> component by react-router-dom . The NavLink component provides a declarative way to navigate around the application. It is similar to the Link component, except it can apply an active style to the link if it is active.

Can we use onClick ON LINK IN React?

To set an onClick listener on a link in React:Set the onClick prop on the link. The function you pass to the prop will get called every time the link is clicked.


1 Answers

There are couple ways how you can solve this issue

  1. String concatenation

    <a href={ '#/systemlist?projId=' + project.id }>{project.name}</a>
    
  2. ES2015 string templates

    <a href={ `#/systemlist?projId=${project.id}` }>{project.name}</a>
    

Example

like image 168
Oleksandr T. Avatar answered Oct 04 '22 20:10

Oleksandr T.