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

164
Visualizações
React: Pass function with a given argument to child

My goal is to create a function in a parent component and pass it to the child component, so I can update state from child to parent.

However, I would like to determine one argument in the parent component already.

Is there any way of doing so? See my example below.

Let´s assume I have the following code

const Parent = ( props ) => {
    
    const [counter, setCounter] = useState(0);

    function updateFunc(type, id) {
        let obj = {type : "", id : id}
        if (type == "red"){
            obj.type = 'RED_FLAG';
        } else {
            obj.type = 'ELSE_FLAG';
        }
    return obj;
    }
    
    return (
        <>
            // HERE I WOULD LIKE TO PASS ALSO A "RED" ARGUMENT -> IS THIS POSSIBLE?
            <Child update = {update}
        </>
    )

}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can do that by making an additional function:

<Child update={(id) => {
  return updateFunc("red", id);
}}/>

about 4 years ago · Juan Pablo Isaza Relatório

0

There is a technique called currying. For example, you could have a function that takes the type as an argument and returns a function that takes the id as an argument, which finally returns the object.

const Parent = (props) => {

    const [counter, setCounter] = useState(0);

    function updateFunc(type) {
        return (id) => {
            let obj = { type: "", id: id }
            if (type == "red") {
                obj.type = 'RED_FLAG';
            } else {
                obj.type = 'ELSE_FLAG';
            }
            return obj;
        }
    }

    return (
        <>
            <Child update={update("red")}
        </>
    )

}
about 4 years ago · Juan Pablo Isaza 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