reactjs - 关于 React 上下文:“{}”类型中缺少属性“children”,但在“ConsumerProps”类型中是必需的
问题描述
我是 React Context 的新手,下面的代码可以工作,但总是会出现类型错误。
<AppContext.Consumer>
{({ value }: { value: string }) => <div>{value}</div>}
</AppContext.Consumer>
“{}”类型中缺少属性“children”,但在“ConsumerProps<...>”类型中是必需的。ts(2741) index.d.ts(342, 9):此处声明了“children”。
interface ConsumerProps<T> {
children: (value: T) => ReactNode;
}
我认为{({ value }: { value: string }) => <div>{value}</div>}
应该被检测为只是 ConsumerProps 想要的孩子,但事实并非如此。如果我写这样的代码
<AppContext.Consumer
children={() => {
return <View />
}}
/>
有一个 eslint 约束
请勿将儿童作为道具传递。相反,在开始标签和结束标签之间嵌套子元素。eslintreact/no-children-prop
我怎么能在不改变 eslint 约束的情况下解决这个问题。
解决方案
推荐阅读
- junit - @AcitiveProfiles 适用于除“test”之外的任何后缀,用于加载 application-test.yml
- spring-integration - 弹簧集成 | 响应式流支持 | 创建反应式消息网关时出现异常
- python - 一个可变数量 (n) 的嵌套 for 循环,其中 n 是 Python 中类属性的数量?
- oracle - 将 KafkaConnect JdbcDriver 与 Oracle 11g(Strimzi 运算符)一起使用时出现无用的 ORA 错误
- c - 如何在C中除以整数并获得浮点数
- anaconda - 通过 conda 安装软件包的“conda install anaconda”和“conda install conda-forge”两种方法有什么区别?
- python - 未找到“结果”的反向。“结果”不是有效的视图函数或模式名称
- vb.net - 具有多个上下文的重复表类的问题
- rust - 如何让结构拥有 chrono::format::Item?
- python - 如何在多列上使用 OneHotEncoder 编码?