Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

165
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

0

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda