I have the start of s simple todo app in with React and typescript
I am creating a context but getting an error in the value of the content provider
<TodoContext.Provider value={contextValue}>{children}</TodoContext.Provider>
for the value I get the error
Type '{ todoList: string[]; }' is not assignable to type 'null'.
What does error mean and how can i fix this typescript error
import { createContext, useContext, useState, ReactChildren, ReactChild } from "react";
interface AuxProps {
children: ReactChild | ReactChildren;
}
const TodoContext = createContext(null)
const intialTodo = ['Learn Context']
const TodoProvider = ({children}:AuxProps) => {
const [todoList, setTodoList] = useState<string[]>(intialTodo)
const contextValue = {
todoList
}
return(
<TodoContext.Provider value={contextValue}>{children}</TodoContext.Provider>
)
}
export const useTodoContext = () => useContext(TodoContext)
export default TodoProvider
Because you are sending just null. but context needs null with type here like that
const TodoContext = createContext(null as any)
render{
const photosObject = this.context as any
}
Why are you assigning null to string array?
const TodoContext = createContext([])
does it work for your case?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With