[英]React Typescript, Type '' is not assignable to type 'null'
I have the start of s simple todo app in with React and typescript我使用 React 和 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错误是什么意思,我该如何解决这个 typescript 错误
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
Why are you assigning null to string array?为什么将 null 分配给字符串数组?
const TodoContext = createContext([])
does it work for your case?它适用于您的情况吗?
Because you are sending just null.因为您只发送 null。 but context needs null with type here like that
但是上下文需要 null 类型在这里
const TodoContext = createContext(null as any)
render{
const photosObject = this.context as any
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.