Im using typescript, not too familiar with and am getting the following err for when I use context and useReducer hook- in particularly for my dispatch method I get this:
contextProps must have a '[Symbol.iterator]()' method that returns an iterator.
I tried to research it but can't figure out what to do. I imagine its because of const [state, dispatch] = useContext(GlobalState); and I need to specify something because of the array. Also, Im getting a ts err for value in
<GlobalState.Provider value={[state, dispatch]}>
(see in code below). Somehow when I have it an array, all properties I have defined in ContextProps are missing. Any help would be so helpful!
//context.js file
export interface ProjectProps {
projects: string[]
}
export interface ContextProps extends ProjectProps {
show: boolean
posts: string[]
//...
}
const initialState = {
posts: [],
show: false
//...
};
export const GlobalState = React.createContext<ContextProps | null>(
initialState
)
const reducer = (state, action) => {
switch (action.type) {
case "SHOW":
return { ...state, show: !state.show }
default:
return state
}
}
const Store: React.FC<{ children: React.ReactNode }> = ({children}) => {
const [state, dispatch] = useReducer(Reducer, initialState);
return (
<GlobalState.Provider value={[state, dispatch]}>
{children}
</GlobalState.Provider>
)
};
const App = () => {
return (
<Store>
<Header/>
<Blog/>
</Store>
);
};
const Blog = () => {
const [state, dispatch] = useContext(GlobalState);
return (
<div>
<button onClick={dispatch({type: "SHOW"})}></button>
{state.show ? <p>hello</p> : null }
</div>
);
};