DEFINITION::::::Context provide a way to pass data through the component tree without
having to pass down manually at every level
HOWTOUSE::::::DECLARATION:::constMyContext=React.createContext()Creating a newContextfor each unique piece of data that needs to be available
throughout your component data
constLocaleContext=React.createContext()PropertiesofLocaleContext--------LocaleContext.ProviderLocaleContext.ConsumerWhat is a ProviderAllows us to "declare the data that we want available throughout our
component tree"
What is a ConsumerAllows "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";constNoteContext=createContext();exportdefaultNoteContext;// In NoteState.js or whatever name you want to giveimportNoteContextfrom"./NoteContext";constNoteState=(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>)}exportdefaultNoteState;This is all about ContextAPI how to create it and add it to ComponentsNow how to use it in the Components:-importReact,{ useContext }from'react';importNoteContextfrom"../contexts/notes/NoteContext";constAbout=()=>{const contextValue =useContext(NoteContext);return(
you can simply use here.)}exportdefaultAbout
// In App.js <NoteState>
components where you want to use this context which you created
</NoteState>// In NoteContext.js import{ createContext }from"react";constNoteContext=createContext();exportdefaultNoteContext;// In NoteState.js or whatever name you want to giveimportNoteContextfrom"./NoteContext";constNoteState=(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>)}exportdefaultNoteState;This is all about ContextAPI how to create it and add it to ComponentsNow how to use it in the Components:-importReact,{ useContext }from'react';importNoteContextfrom"../contexts/notes/NoteContext";constAbout=()=>{const contextValue =useContext(NoteContext);return(
you can simply use here.)}exportdefaultAbout
classAppextendsReact.Component{render(){return<Toolbar theme="dark"/>;}}functionToolbar(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>);}classThemedButtonextendsReact.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";constNoteContext=createContext();exportdefaultNoteContext;// In NoteState.js or whatever name you want to giveimportNoteContextfrom"./NoteContext";constNoteState=(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>)}exportdefaultNoteState;This is all about ContextAPI how to create it and add it to ComponentsNow how to use it in the Components:-importReact,{ useContext }from'react';importNoteContextfrom"../contexts/notes/NoteContext";constAbout=()=>{const contextValue =useContext(NoteContext);return(
you can simply use here.)}exportdefaultAbout