Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

110
Vistas
How do I parse a state variable's value into a <Link> element's target url in React?

I have been trying to call the profile link using a state variable id, to navigate to a specific user's profile, the method below doesn't work but if I am able to put the 'id' value into the url as I am trying to do below it will work because in App.js:

<Route path="/user/:id" component={Profile} />

And I need the link below to be clickable on the home page to take a logged in user to their profile by fetching their id from the server and saving it in a state variable and then parsing the value into the url dynamically. Home.js:

 constructor(props) {
            super(props);
            id: localStorage.getItem("id");
        }
    
        componentDidMount() {
            const payload = {
                "username": localStorage.getItem("username")
            };
    
            fetch(ID_FROM_USERNAME_URL, {
                method: 'POST',
                headers: {
                    "Authorization": localStorage.getItem("token"),
                    "Accept": "application/json",
                    "Content-Type": "application/json"
                },
                body: JSON.stringify(payload)
            })
                .then(response => response.json())
                .then((data) => {
                    console.log("id from server:")
                    console.log(data);
                    localStorage.setItem("id", data);
                });
        }

In render() return ()

    <Button color=

"link"><Link to="/user/${this.state.id}">Profile</Link></Button>`

How can I parse the state variable's value into this Link element?

Thanks

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

you are using template literal wrong.. it need to be passed as variable and in backticks

<Link to={`/user/${this.state.id}`}>Profile</Link></Button>

about 4 years ago · Juan Pablo Isaza Denunciar

0

You need to use template literal, it's a nice feature added in ES6.

<Link to={`/user/${this.state.id}`}>Profile</Link></Button>

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

Also, it looks like your code is broken, you don't have state and setState and it's not wise to use storage for this task.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda