Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
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 answers
Answer question

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 Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!