Une autre solution consiste à créer un contexte distinct fournissant les autres contextes :
import React, { createContext, memo, useContext } from "react";
import isEqual from "react-fast-compare";
export const MultiContext = createContext(null);
MultiContext.displayName = "MultiContext";
export const MultiContextProvider = memo(
function({ map, children }) {
const contextMap = {};
for (const i in map) {
contextMap[i] = useContext(map[i]);
}
return (
<MultiContext.Provider value={contextMap}>
{children}
</MultiContext.Provider>
);
},
(prevProps, nextProps) => isEqual(prevProps.children, nextProps.children)
);
MultiContextProvider.displayName = "MultiContextProvider";
Exemple d'utilisation :
class DemoConsumer extends React.Component {
static contextType = MultiContext;
render() {
return JSON.stringify({
someValue: this.context.SomeContext.someValue,
otherValue: this.context.OtherContext.otherValue,
});
}
}
function App() {
return (
<MultiContextProvider map={{ SomeContext, OtherContext }}>
<MultiContextDemoClassConsumer />
</MultiContextProvider>
);
}
Démonstration :
const {
createContext,
memo,
useContext,
useState,
useEffect,
} = React;
const MultiContext = createContext(null);
MultiContext.displayName = "MultiContext";
const MultiContextProvider = memo(
function({ map, children }) {
console.log("render provider");
const contextMap = {};
for (const i in map) {
contextMap[i] = useContext(map[i]);
}
return (
<MultiContext.Provider value={contextMap}>
{children}
</MultiContext.Provider>
);
},
(prevProps, nextProps) => isEqual(prevProps.children, nextProps.children)
);
MultiContextProvider.displayName = "MultiContextProvider";
const initialMinutes = new Date().getMinutes();
const MinutesContext = createContext(initialMinutes);
MinutesContext.displayName = "MinutesContext";
const IncrementContext = createContext(0);
IncrementContext.displayName = "IncrementContext";
class MultiContextDemoClassConsumer extends React.Component {
static contextType = MultiContext;
render() {
return JSON.stringify(this.context);
}
}
const multiContextMap = { MinutesContext, IncrementContext };
function App() {
const forceUpdate = useForceUpdate();
const [minutes, setMinutes] = useState(initialMinutes);
useEffect(() => {
const timeoutId = setInterval(() => {
// console.log('set minutes')
setMinutes(new Date().getMinutes());
}, 1000);
return () => {
clearInterval(timeoutId);
};
}, [setMinutes]);
const [increment, setIncrement] = useState(0);
console.log("render app");
return (
<MinutesContext.Provider value={minutes}>
<IncrementContext.Provider value={increment}>
<MultiContextProvider map={multiContextMap}>
<MultiContextDemoClassConsumer />
</MultiContextProvider>
<button onClick={() => setIncrement(i => i + 1)}>Increment</button>
<button onClick={forceUpdate}>Force Update</button>
</IncrementContext.Provider>
</MinutesContext.Provider>
);
}
ReactDOM.render(<App />, document.getElementById("root"));
<script type="module">
import React from 'https://dev.jspm.io/react@16';
import ReactDOM from 'https://dev.jspm.io/react-dom@16';
import useForceUpdate from 'https://dev.jspm.io/use-force-update@1.0.7';
import isEqual from 'https://dev.jspm.io/react-fast-compare@3.0.1';
window.React = React;
window.ReactDOM = ReactDOM;
window.useForceUpdate = useForceUpdate.default;
window.isEqual = isEqual;
</script>
<div id="root"></div>