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

156
Visualizações
CreateContext hook is not passed data to child component on button click event

I have a parent component in which on button click event i passed the data to child component using CreateContext and get it using useContext hook in child component but it is not passed to child component please solve it thanks, check codes below import React, { useState, createContext, useContext } from "react"; const FormContext = createContext();

function Parent() {
  const [name, setName] = useState("");
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const obj = { name, email, password };
  const handleClick = () => {
    <FormContext.Provider value={obj}>
      <Child />
    </FormContext.Provider>;
  };
  return (
    <div>
      <input type="text" onChange={(e) => setName(e.target.value)} />
      <br />
      <input type="text" onChange={(e) => setEmail(e.target.value)} />
      <br />
      <input type="text" onChange={(e) => setPassword(e.target.value)} />
      <br />
      <button onClick={handleClick}>Click me</button>
    </div>
  );
}
const Child = () => {
  const data = useContext(FormContext);
  return (
    <>
      <div>{JSON.stringify(data)}</div>
    </>
  );
};
export default Parent;
````````````````````````````````````````````````````````````````
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

It looks like in your example the Child -component is not rendered at all.

If your plan is to show the Child-component after you have clicked the "Click me" -button, then you could render it conditionally and pass in the data directly without using the context-api. Like this:

    {showChild && 
        <Child data={{name, email, password}}></Child>
    }

You need to add state -variable showChild and set it true in the handleClick -function.

Below is a working example:

    function Parent() {
      const [name, setName] = useState("");
      const [email, setEmail] = useState("");
      const [password, setPassword] = useState("");
      const [showChild, setShowChild] = useState(false);
    
      const handleClick = () => {
        setShowChild(true);
      };

      return (
        <div>
          <input type="text" onChange={(e) => setName(e.target.value)} />
          <br />
          <input type="text" onChange={(e) => setEmail(e.target.value)} />
          <br />
          <input type="text" onChange={(e) => setPassword(e.target.value)} />
          <br />
          <button onClick={handleClick}>Click me</button>
          {showChild && 
            <Child data={{name,  email, password}}></Child>
          }
        </div>
      );
    }

    const Child = (props) => {
      const data = props.data;
      return (
        <>
          <div>{JSON.stringify(data)}</div>
        </>
      );
    };
    export default Parent;

If you really have to use the Context-api, then you need to wrap the child-component to to a FormContext.Provider and pass obj as a value property. Like this:

       <FormContext.Provider value={obj}>
          <Child></Child>
        </FormContext.Provider>

Below is a working example:


    function Parent() {
      const [name, setName] = useState("");
      const [email, setEmail] = useState("");
      const [password, setPassword] = useState("");
      const [showChild, setShowChild] = useState(false);
      const obj = { name, email, password };
    
      const handleClick = () => {
        setShowChild(true);
      };
    
      return (
          <div>
            <input type="text" onChange={(e) => setName(e.target.value)} />
            <br />
            <input type="text" onChange={(e) => setEmail(e.target.value)} />
            <br />
            <input type="text" onChange={(e) => setPassword(e.target.value)} />
            <br />
            <button onClick={handleClick}>Click me</button>
            {showChild &&
           <FormContext.Provider value={obj}>
              <Child></Child>
            </FormContext.Provider>
            }
          </div>
      );
    }
    const Child = () => {
      const data = useContext(FormContext);
      return (
        <>
          <div>{JSON.stringify(data)}</div>
        </>
      );
    };
    export default Parent;

about 4 years ago · Juan Pablo Isaza Relatório

0

Try this . <FormContext.Provider value={{obj}}> <Child /> </FormContext.Provider>;

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