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

297
Visualizações
How to call a Function in UseEffect?

I want to call below mentioned function onSaveInputValue into the useEffect so that, my component can have passed value in childInputValue hook whenever the page gets loaded.

    const onSaveInputValue = (value) => {
        setChildInputValue(value);
        console.log(childInputValue);
      };

I have tried to do so but couldn't do it. Please guide me.

In case if you like to see the full code of components then it is mentioned as below.

Parent Component:-

import { useEffect, useState } from "react";
import Child from "./Child";

const Parent = () => {
  const [childInputValue, setChildInputValue] = useState();
  // useEffect(() => {});
    const onSaveInputValue = (value) => {
    setChildInputValue(value);
    console.log("childVInputValue",value)
  };

  return (
    <div>
      <h1>Parent Component</h1>
      <p>{childInputValue}</p>

      {/* We pass function to the child component. */}
      <Child onSaveInputValue={onSaveInputValue} />
    </div>
  );
};

export default Parent;

Child Component:-

import React, { useEffect, useState } from "react";
import "./App.css";

function Child(props) {
  const [baseImage, setBaseImage] = useState("");

  const uploadImage = async (e) => {
    const file = e.target.files[0];
    const base64 = await convertBase64(file);
    setBaseImage(base64);
    props.onSaveInputValue(baseImage);
  };

  const convertBase64 = (file) => {
    return new Promise((resolve, reject) => {
      const fileReader = new FileReader();
      fileReader.readAsDataURL(file);

      fileReader.onload = () => {
        resolve(fileReader.result);
      };

      fileReader.onerror = (error) => {
        reject(error);
      };
    });
  };


  useEffect(()=>{
    console.log("useeeffect",baseImage)
  })

  return (
    <div >
      <input
        type="file"
        onChange={(e) => {
          uploadImage(e);
        }}
      />
      <br></br>
      <img src={baseImage} height="200px" />
      <hr/>
      
    </div>
  );
}

export default Child;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can remove a lot of stuff

import { useEffect, useState } from "react";
import Child from "./Child";

const Parent = () => {
  const [childInputValue, setChildInputValue] = useState(null);

  return (
    <div>
      <h1>Parent Component</h1>
      <p>{childInputValue}</p>

      {/* We pass function to the child component. */}
      <Child onSaveInputValue={setChildInputValue} />
    </div>
  );
};

export default Parent;
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