I have a next.js app where I am trying to use react context to export a state. However, the state is not updating and remains as the initial value set by the createContext hook which is undefined in this case. Am I doing something wrong?
This is the file where I create the context:
import { NextPage } from 'next';
import {
useState,
SyntheticEvent,
createContext,
useContext,
ReactNode,
} from 'react';
import { TimeZoneType } from '../CompetitionList/types';
interface Props {
children: ReactNode;
}
type ContextType = {
timeZone: TimeZoneType;
handleTimeZone: (_event: SyntheticEvent, _newZone: TimeZoneType) => void;
};
const TimeZone = createContext<ContextType>({} as ContextType);
const TimeZoneState: NextPage<Props> = ({ children }) => {
const [timeZone, setTimeZone] = useState<TimeZoneType>('Local');
const handleTimeZone = (_event: SyntheticEvent, newZone: TimeZoneType) => {
if (newZone !== null) {
setTimeZone(newZone);
}
};
return (
<TimeZone.Provider value={{ timeZone, handleTimeZone }}>
{children}
</TimeZone.Provider>
);
};
export const useTimeZone = () => {
return useContext(TimeZone);
};
I call the context similar to this:
import { useTimeZone } from '../components/GlobalVariables/TimeZone';
...
const { timeZone, handleTimeZone } = useTimeZone();
console.log({timeZone})
//returns undefined
Sorry if this is a bad question it is my first one.