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

167
Vistas
Is there a way I can pick the value rendered on a Typography in Material ui and assign it to a state?

I have a data array that I am mapping onto a material-ui Typography.

{
  array.map((item, id)=> <Typography key={id} value={item.name} />)
}

The code displays the typography with their respective values on the browser as expected, however, I am trying to set the mapped value of Typography into a state like this...

const [data, setData] = useState();

...

{
 array.map((item, id) => 
      <Typography key={id} value={item.name} {(e)=>setData(e.target.value)} />
)}

This method does not work. How do I make data to be the value of <Typography/>

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

0

If I understand correctly, you're trying to store item.name within local state? How are you receiving the data that you are mapping through?

Are you trying to store each item you're mapping through and represent key-value pairs of an object within local state? What are you trying to do with the state once you have it?

As far as I know, there's is no way to assign values to local state while you are mapping through an array. However, there are multiple ways to assign the data to local state outside of the return.

Also, what is this line doing?

{(e)=>setData(e.target.value)}

It seems you're trying to create a controlled input on a Typography component without an onChange prop, as well as on a component that does not take input.

One more aside, although it is not a big deal -

{
 array.map((item, id) => 
      <Typography key={id} value={item.name} ..../>
)}

id here is usually referred to as index


Edited for answer

I normally use Redux to manage the global state, but the process should be the same for Context Provider:

const yourComponent = () => {

  const [data, setData] = useState();

  // This is the array you get from the Context Provider
  const yourArray = getArrayFromContextProvider();

  useEffect(() => {
    // If you want to normalize the array data
    if (yourArray) {
      const dataObj = {};
      yourArray.forEach((item, index) => {
        // Make the key unique by using the item id OR you can use index
        dataObj[item.id] = item;
        // OR you can add the item value as the key
        dataObj[item.value] = item;
      });
      setData(dataObj);
    }
    // If you just want to store the array
    if (yourArray) {
       setData(yourArray)
    }
  }, [yourArray]);

  return <div>
    {/* ...Your map */}
  </div>;
};

export default yourComponent
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