Listing 17-3: Using a Provider Component
import React, {useState} from 'react';
import {PrefsContext} from './contexts/UserPrefs';
const UserPrefsProvider = ({ children }) => {
const [lang, setLang] = useState("English");
const [timezone, setTimezone] = useState("UTC");
return (
<PrefsContext.Provider value={{ lang, timezone }}>
{children}
</PrefsContext.Provider>
);
};
function App(){
return (
<UserPrefsProvider>
<Header />
<Main />
<Footer />
</UserPrefsProvider>
)
}
export default App;
Download the examples, report issues, and ask/answer questions in the discussion area by visiting the book's github page. All of the code for the book is also available on codesandbox.io for you to play around with.
ReactJS Foundations is published by John Wiley and Sons, Inc and is available in paperback and eBook.