Muhammad Khan
Muhammad Khan

Reputation: 7

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;
````````````````````````````````````````````````````````````````

Upvotes: 1

Views: 415

Answers (2)

Gwydion
Gwydion

Reputation: 407

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;

Upvotes: 2

Osama Bin Sajid
Osama Bin Sajid

Reputation: 52

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

Upvotes: 0

Related Questions