I'm trying to set up a context provider for my Next.js application, in Typescript.
I've set up a context provider before in React in plain JS, so this is my shot at learning TS.
In the code below, the value={}
prop of Context.Provider
returns the error:
Property 'tasks' is missing in type '(State | Dispatch<SetStateAction<State>>)[]' but required in type 'State'
import { createContext, useState } from "react";
import { ReactNode } from "react";
interface Task {
name: string,
finished: boolean
}
interface State {
tasks: Task[]
}
const initialState: State = {
tasks: []
}
export const Context = createContext<State>(initialState)
type stateProps = {
children: ReactNode
}
const StateContext = ({ children }: stateProps) => {
const [state, setState] = useState<State>(initialState)
return (
<Context.Provider value={[state, setState]}>
{children}
</Context.Provider>
)
}
export default StateContext;
For some reason, it's saying that tasks
is missing in something that has the type (State | Dispatch<SetStateAction<State>>)[]
, I have no clue where.
I've ran into lots of other issues while learning TS, but for some reason I can't figure this one out. Any ideas?