Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

277
Visualizações
pass an array from a child to parent ReactJS

i have an array in a component i want to export it or pass it to other component (parent)

child component :

export default function child(){

const arr = ["1020","52214","3325"]
return (
<div> child</div>
)
}

parent :

export default function parent(){

return (
<>
<div> parrent</div>
<child/>
</>
)
}

almost 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

In react we don't pass values from child to parent but the other way around: it's a one-way data flow!

You can useState to create a stateful value and a set-function in the parent, pass the set function to the child and allow it to set the value.

Example:

export default function child(props){

const arr = ["1020","52214","3325"];
props.setMyArray(arr);

return (
<div> child</div>
)
} 

import { useState } from 'react';

export default function parent(){
  const [myArray, setArray] = useState([]);

  return (
    <>
    <div> parrent</div>
    <child setMyArray={setArray}/>
    </>
    )
  }
}
almost 4 years ago · Santiago Trujillo Relatório

0

You can't pass data from child to parent.

You can pass like this.

const ParentPage = () => {

  const [arr , setArr ] = useState([])
  console.log(arr)  // [1,2,3]

  return (
    <ChildPage setArr={setArr} />
  );
}

const ChildPage = ({setArr}) => {
    return (
        <button onClick={()=> setArr([1,2,3]) }}> </button>
    );
}

or you can do with redux

almost 4 years ago · Santiago Trujillo Relatório

0

You can use props to call a function in the parent element and pass an argument in the function from the child element.

Here's an example:

Child element:

export default function child(props){

const [arr,setarr]= useState(["1020","52214","3325"]);

return (
<div onclick={()=>{props.func(arr)}}> child</div>
)
}

Parent element:

export default function parent(){
 
function passvalue(arr){
   const passedvalue = arr; 
}


return (
<>
<div> parrent</div>
<child func={passvalue}/>
</>
)
}
almost 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda