-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
useContext to access data from any child components
- Loading branch information
1 parent
10de4e8
commit 75a9b2c
Showing
2 changed files
with
22 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,22 @@ | ||
import React, { createContext } from "react"; | ||
import ReactDOM from "react-dom"; | ||
import "./index.css"; | ||
import App from "./App"; | ||
|
||
export const TreesContext = createContext(); | ||
export const HeadachesContext = createContext(); | ||
|
||
const trees = [ | ||
//make all this data acessible to all app | ||
const headaches = [ | ||
{ id: "1", type: "migraines" }, | ||
{ id: "2", type: "digestive problem" }, | ||
{ id: "3", type: "sinus headaches" }, | ||
{ id: "4", type: "tension" } | ||
]; | ||
export default function App() { | ||
return ( | ||
<div> | ||
<h1>Types of headaches</h1> | ||
</div> | ||
); | ||
} | ||
|
||
// Pass data down to the child components | ||
ReactDOM.render( | ||
<TreesContext.Provider value={{ trees }}> | ||
<HeadachesContext.Provider value={{ headaches }}> | ||
<App /> | ||
</TreesContext.Provider>, | ||
</HeadachesContext.Provider>, | ||
document.getElementById("root") | ||
); |