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

185
Vistas
Passing values from react components to constants file

I've moved all my static text to the constants file and I've to pass user's first name and last name to one particular key. I've tried using string literal but its not working. Is there any way to pass the values from react to the constants file.

constants.js

export constants={
'employeeName': `Hello welcome ${employee.firstname}, ${employee.lastname}`
}

React component

import constants from './constants.js'

const employee={
firstname:props.firstname,
lastname: props.lasname
}

<div>{constants.employeeName}</div>
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

If you insist to use a string constants in separate file and use and format it in another component you can use the parse method from this post or some string format library like hydrate-text like this:

constants.js

export const constants = {
  employeeName: `Hello welcome %s, %s`,
};

util.js;

export function parse(str) {
  var args = [].slice.call(arguments, 1),
    i = 0;

  return str.replace(/%s/g, () => args[i++]);
}

component:

import { parse } from "util";
import constants from "./constants.js";

const Employee = () => {
  const employee = {
    firstname: props.firstname,
    lastname: props.lasname,
  };

  return (
    <div>
      {parse(
        constants.employeeName,
        employee.firstname,
        employee.lastname
      )}
    </div>
  );
};

In my opinion it's not a clean and performant way of doing this, you can just use a function and pass employee to it like the other answers:

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can create a method and pass an argument on that.

Example :

    const getFullName = (employee) => {
     return {
       'employeeName': `Hello welcome ${employee.firstname}, ${employee.lastname}`
     }
    };
    
    const employee = {
     firstname: "asif",
     lastname: "vora"
    };
    
    const { employeeName } = getFullName(employee);

    console.log(employeeName);

about 4 years ago · Juan Pablo Isaza Denunciar

0

constant.js:

export const constants= { employeeName: 'Hello welcome'}

React Component:

import constants from './constants.js'

const employee={firstname:props.firstname,lastname: props.lasname}

<div>{`${constants.employeeName} ${employee.firstname}, ${employee.firstname}`}</div>
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