React useContext

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

  1. Create the GlobalContextProvider at /src/context/GlobalContextProvider.js which will create all your contexts and provide API’s to maintain the state.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import React, { useState, createContext, useContext } from 'react';

const GlobalSpinnerContext = createContext();
const GlobalSpinnerActionsContext = createContext();

/* eslint-disable */
const useContextFactory = (name, context) => {
return () => {
const ctx = useContext(context);
if (ctx === undefined) {
throw new Error(
`use${name}Context must be used withing a ${name}ContextProvider.`
);
}
return ctx;
};
};
/* eslint-enable */

// eslint-disable-next-line react-hooks/rules-of-hooks
export const useGlobalSpinnerContext = useContextFactory(
'GlobalSpinnerContext',
GlobalSpinnerContext
);
// eslint-disable-next-line react-hooks/rules-of-hooks
export const useGlobalSpinnerActionsContext = useContextFactory(
'GlobalSpinnerActionsContext',
GlobalSpinnerActionsContext
);

const GlobalContextProvider = (props) => {
const [isGlobalSpinnerOn, setGlobalSpinner] = useState(false);

return (
<GlobalSpinnerContext.Provider value={isGlobalSpinnerOn}>
<GlobalSpinnerActionsContext.Provider value={setGlobalSpinner}>
{props.children}
</GlobalSpinnerActionsContext.Provider>
</GlobalSpinnerContext.Provider>
);
};

export default GlobalContextProvider;
  1. Wrap your application
1
2
3
4
5
6
7
8
9
10
function App() {
return (
<GlobalSpinnerContextProvider>
<div className="App">
<GlobalSpinner />
<RandomComments />
</div>
</GlobalSpinnerContextProvider>
);
}
  1. Access the contexts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import React from 'react'
import './globalSpinner.css'
import {useGlobalSpinnerContext} from '../../context/GlobalSpinnerContext'

const GlobalSpinner = props => {
const isGlobalSpinnerOn = useGlobalSpinnerContext()
return isGlobalSpinnerOn ? (
<div className="global-spinner-overlay">
<p>Loading...</p>
</div>
) : null
}

export default GlobalSpinner
  1. Access the actions to change the state
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import React, {useState, useEffect } from 'react'
import {useGlobalSpinnerActionsContext} from '../context/GlobalSpinnerContext'


const RandomComments = props => {
const [comments, setComments] = useState([])
const setGlobalSpinner = useGlobalSpinnerActionsContext()
useEffect(() => {
(async () => {
setGlobalSpinner(true)
const result = await fetch('https://jsonplaceholder.typicode.com/comments?l=2')
const data = await result.json()
setComments(data.slice(0, 3))
setGlobalSpinner(false)
})()
}, [setGlobalSpinner])

return (
<div>
{comments.map(comment => {
const {name, body, id} = comment
return (
<div key={id}>
<p style={{fontWeight: 'bold'}}>{name}</p>
<p> {body}</p>
</div>
)
})}
</div>
)
}

export default RandomComments

References