Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
¿Cómo enviar una propiedad desde una matriz de objetos desde un componente secundario al componente principal?

Tengo la App , que es el componente principal y tengo el componente Child :

El componente Child obtiene accesorios llamados items para que pueda reutilizarse según los datos. En el ejemplo hay data , data1 y data2 .

El caso es que quiero configurar una cookie del componente principal, para configurar la cookie necesito el link de propiedad de data2 , pero ya estoy mapeando data2 en el Child component secundario.

¿Qué puedo hacer para obtener el value of the property link en el componente principal para pasarlo como argumento aquí?

 <Child onClick={() => handleUpdate('How can I obtain here the string from link of data2?') } items={data2} />

Este es el código de ejemplo completo:

 import * as React from 'react'; import './style.css'; const data = [ { title: 'hey', description: 'description' }, { title: 'hey1', description: 'description' }, { title: 'hey2', description: 'description' }, ]; const data1 = [ { title: 'hey', description: 'description' }, { title: 'hey1', description: 'description' }, { title: 'hey2', description: 'description' }, ]; const data2 = [ { title: 'hey', link: 'link/hey' }, { title: 'hey1', link: 'link/he1' }, { title: 'hey2', link: 'link/he2' }, ]; export default function App() { const [, setCookie] = useCookie('example'); const handleUpdate = (cookie) => { setCookie(null); setCookie(cookie); }; return ( <div> <h2>App - Parent</h2> <Child items={data} /> <Child items={data1} /> <Child onClick={() => handleUpdate('How can I obtain here the string from link of data2?') } items={data2} /> </div> ); } export function Child({ items }) { return ( <div> <h2>Child</h2> <ul> {items.map((item) => { return ( <> <p>{item.title}</p> <a href={item.link}>Go to title</a> </> ); })} </ul> </div> ); }

¡Gracias!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

El método de map no cambia la matriz a la que se llama, simplemente devuelve una nueva matriz, la matriz de items no se ve afectada en absoluto aquí, por lo que puede llamarla normalmente así:

 return ( <div> <h2>App - Parent</h2> <Child items={data} /> <Child items={data1} /> <Child onClick={() => handleUpdate(data2[0].link) } items={data2} /> </div> );

Además, su componente onClick Child un accesorio de esta manera:

 export function Child({ items, handleClick }) { return ( <div onClick={handleClick}> <h2>Child</h2> <ul> {items.map((item) => { return ( <> <p>{item.title}</p> <a href={item.link}>Go to title</a> </> ); })} </ul> </div> ); }
about 4 years ago · Juan Pablo Isaza Report

0

Si desea obtener el Child del componente secundario, simplemente puede agregar un parámetro de link en la devolución de llamada:

 <Child onClick={(link) => handleUpdate(link)} items={data2} />

Luego, desde el Child , solo necesita llamar al accesorio onClick :

 export function Child({ items, onClick }) { // here make sure to add the prop while destructuring
 <a href={item.link} onClick={() => onClick(item.link)}>Go to title</a>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!