Last active
May 2, 2024 16:57
-
-
Save adityavijay21/7de82ab85f6e455ff49a315c77fd7391 to your computer and use it in GitHub Desktop.
Context API
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 './App.css' | |
import './ChildC.jsx' | |
import {createContext} from "react"; | |
import ChildC from "./ChildC.jsx"; | |
const data = createContext() | |
const data1 = createContext() | |
function App() { | |
const age = 23; | |
const name = "Aditya Vijay"; | |
return ( | |
<> | |
<data.Provider value={name}> | |
<data1.Provider value={age}> | |
<ChildC /> | |
</data1.Provider> | |
</data.Provider> | |
</> | |
) | |
} | |
export default App | |
export {data, data1}; |
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
function ChildA (){ | |
return( | |
<> | |
</> | |
} | |
export default ChildC; |
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
function ChildB (){ | |
return( | |
<> | |
</> | |
} | |
export default ChildB; |
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 {data,data1} from './App.jsx'; | |
function ChildC (){ | |
return( | |
<> | |
<data.Consumer> | |
{ | |
(name) => { | |
return( | |
<data1.Consumer> | |
{ | |
(age) => { | |
return( | |
<h1>I am {name} and my age is {age}.</h1> | |
) | |
} | |
} | |
</data1.Consumer> | |
) | |
} | |
} | |
</data.Consumer> | |
</> | |
) | |
} | |
export default ChildC; |
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
- / | |
- src | |
- App.jsx | |
- ChildA.jsx | |
- ChildB.jsx | |
- ChildC.jsx |
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
we use context api to avoid the prop drilling. | |
The Context API in React helps avoid prop drilling by passing global variables throughout the codebase. | |
However, it can lead to nested consumers and callback hell when dealing with numerous data providers. To address this, utilize the 'useContext' React Hook for a cleaner solution |
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
npm run dev |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment