When you dont need a store (yet) you can just use useContext
, this hook helps avoid prop drilling and components taking a dependency on props they dont use other than passing to children components.
The code below is based on telerik.com: How to Use Context API and the complete source code is at https://github.com/carlpaton/react-usecontext
- Create the
GlobalContextProvider
at/src/context/GlobalContextProvider.js
which will create all your contexts and provide API’s to maintain the state.
1 | import React, { useState, createContext, useContext } from 'react'; |
- Wrap your application
1 | function App() { |
- Access the contexts
1 | import React from 'react' |
- Access the actions to change the state
1 | import React, {useState, useEffect } from 'react' |