DEFINITION::::::
Context provide a way to pass data through the component tree without
having to pass down manually at every level
HOW TO USE::::::
DECLARATION:::
const MyContext = React.createContext()
Creating a new Context for each unique piece of data that needs to be available
throughout your component data
const LocaleContext = React.createContext()
Properties of LocaleContext --------
LocaleContext.Provider
LocaleContext.Consumer
What is a Provider
Allows us to "declare the data that we want available throughout our
component tree"
What is a Consumer
Allows "any component in the tree that needs that data to be able to
subscibe to it"
How to use Provider
<MyContext.Provider value={data}>
<App />
</MyContext.Provider>
// In App.js
<NoteState>
components where you want to use this context which you created
</NoteState>
// In NoteContext.js
import { createContext } from "react";
const NoteContext = createContext();
export default NoteContext;
// In NoteState.js or whatever name you want to give
import NoteContext from "./NoteContext";
const NoteState = (props)=>{
// you can also pass state here also or functions also
const user = {
name: "Gourav Khurana",
age: 19,
caast: "General"
}
return (
<NoteContext.Provider value={user}>
{props.children}
</NoteContext.Provider>
)
}
export default NoteState;
This is all about Context API how to create it and add it to Components
Now how to use it in the Components :-
import React, { useContext } from 'react';
import NoteContext from "../contexts/notes/NoteContext";
const About = () => {
const contextValue = useContext(NoteContext);
return (
you can simply use here.
)
}
export default About
// In App.js
<NoteState>
components where you want to use this context which you created
</NoteState>
// In NoteContext.js
import { createContext } from "react";
const NoteContext = createContext();
export default NoteContext;
// In NoteState.js or whatever name you want to give
import NoteContext from "./NoteContext";
const NoteState = (props)=>{
// you can also pass state here also or functions also
const user = {
name: "Gourav Khurana",
age: 19,
caast: "General"
}
return (
<NoteContext.Provider value={user}>
{props.children}
</NoteContext.Provider>
)
}
export default NoteState;
This is all about Context API how to create it and add it to Components
Now how to use it in the Components :-
import React, { useContext } from 'react';
import NoteContext from "../contexts/notes/NoteContext";
const About = () => {
const contextValue = useContext(NoteContext);
return (
you can simply use here.
)
}
export default About
class App extends React.Component {
render() {
return <Toolbar theme="dark" />;
}
}
function Toolbar(props) {
// The Toolbar component must take an extra "theme" prop
// and pass it to the ThemedButton. This can become painful
// if every single button in the app needs to know the theme
// because it would have to be passed through all components.
return (
<div>
<ThemedButton theme={props.theme} />
</div>
);
}
class ThemedButton extends React.Component {
render() {
return <Button theme={this.props.theme} />;
}
}
// In App.js
<NoteState>
components where you want to use this context which you created
</NoteState>
// In NoteContext.js
import { createContext } from "react";
const NoteContext = createContext();
export default NoteContext;
// In NoteState.js or whatever name you want to give
import NoteContext from "./NoteContext";
const NoteState = (props)=>{
// you can also pass state here also or functions also
const user = {
name: "Gourav Khurana",
age: 19,
caast: "General"
}
return (
<NoteContext.Provider value={user}>
{props.children}
</NoteContext.Provider>
)
}
export default NoteState;
This is all about Context API how to create it and add it to Components
Now how to use it in the Components :-
import React, { useContext } from 'react';
import NoteContext from "../contexts/notes/NoteContext";
const About = () => {
const contextValue = useContext(NoteContext);
return (
you can simply use here.
)
}
export default About