Created
January 15, 2020 10:54
-
-
Save Mayankgupta688/b92ddd7602a7de31c2333a88baf2a3fa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from "react"; | |
var userDetailContext = React.createContext(null); | |
export default function UserDetailsComponent() { | |
var [userDetails] = useState({ | |
name: "Mayank", | |
age: 30 | |
}); | |
return ( | |
<userDetailContext.Provider value={userDetails}> | |
<h1>This is the Parent Component</h1> | |
<hr /> | |
<ChildComponent userDetails={userDetails} /> | |
</userDetailContext.Provider> | |
); | |
} | |
function ChildComponent(props) { | |
return ( | |
<div> | |
<h2>This is Child Component</h2> | |
<hr /> | |
<SubChildComponent /> | |
</div> | |
); | |
} | |
function SubChildComponent(props) { | |
var contextData = React.useContext(userDetailContext); | |
return ( | |
<div> | |
<h3>This is Sub Child Component</h3> | |
<h4>User Name: {contextData.name}</h4> | |
<h4>User Age: {contextData.age}</h4> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You created all parent child components in single file because of this userDetailContext is available to all child component what if child component is in another file. How will I get userDetailContext there ?