[英]Typescript ESLint Error - How can I type children when using react functional components?
I am putting together a boilerplate sample of:我正在整理一个样板样本:
I have almost everything worked out, but I can't seem to figure out this one ESLint error I am getting.我几乎已经解决了所有问题,但我似乎无法弄清楚我遇到的这个 ESLint 错误。 I have a MobX store provider that looks like this
我有一个像这样的 MobX 商店提供者
import { useLocalStore } from 'mobx-react';
import React from 'react';
import { StoreType } from '../Types/StoreType';
import { StoreContext } from './StoreContext';
export const StoreProvider = ({ children }) => {
const store = useLocalStore(() => ({
loginStore: { email: ['neil.peart@rush.yyz'] },
applicationStore: { firstName: 'neil', lastName: 'peart' }
})) as StoreType;
return <StoreContext.Provider value={store}>{children}</StoreContext.Provider>;
};
I am getting the error:我收到错误:
6:30 warning Missing return type on function @typescript-eslint/explicit-function-return-type
6:30 警告缺少函数 @typescript-eslint/explicit-function-return-type 的返回类型
6:33 error 'children' is missing in props validation react/prop-types
6:33 错误“儿童”在道具验证反应/道具类型中丢失
If you want to pull the whole thing down, you can here: https://github.com/Savij/functional-react-mobx-material如果你想把整个事情拉下来,你可以在这里: https : //github.com/Savij/functional-react-mobx-material
Appreciate any insight!欣赏任何见解! -J
-J
The return type of StoreProvider
in your example is a React.ReactElement
, which can be set like so:您的示例中
StoreProvider
的返回类型是React.ReactElement
,可以这样设置:
export const StoreProvider = ({ children }): React.ReactElement => {
return ...
}
The type of children
in your source code is also React.ReactElement
, but must be wrapped in an interface because it's getting passed in as a property of the component's props.源代码中的
children
类型也是React.ReactElement
,但必须包装在接口中,因为它作为组件 props 的属性传入。
export const StoreProvider = ({ children }: StoreProviderProps): React.ReactElement => {
return ...
}
interface StoreProviderProps {
children: React.ReactElement;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.