Answers
createContext
must be passed a default value when using it with TypeScript. Here’s the corrected code:const ThemeContext = createContext<ThemeContextType>({
name: 'standard',
color: 'light',
changeTheme: (name: string, color: 'dark' | 'light') => {},
});
Footer
must be placed insideThemeProvider
as follows:<ThemeProvider>
<Header />
<Main />
<Footer />
</ThemeProvider>
- Yes, there is no limit on the number of React contexts in an app.
- No, only a single Redux store can be added to an app.
useDispatch
can’t be used directly to dispatch an action – it returns a function that can be used to dispatch an action:const dispatch = useDispatch();
function handleChangeTheme({ name, color }: Theme) {
dispatch(changeThemeAction(name, color));
}
- Yes, local state defined using
useState
oruseReducer...