简体   繁体   English

Typescript ESLint 错误 - 使用 React 功能组件时如何输入子项?

[英]Typescript ESLint Error - How can I type children when using react functional components?

I am putting together a boilerplate sample of:我正在整理一个样板样本:

  • React 16.x (from create-react-app) React 16.x(来自 create-react-app)
  • Typescript打字稿
    • with functional components带功能组件
  • Material-UI材质界面
  • Mobx-React Mobx-React
    • via context providers通过上下文提供者
  • ESLint ESLint

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.

相关问题 如何使用 react 和 typescript 在反应功能组件中渲染子级? - How to render children within react functional components using react and typescript? 使用 Typescript generics 进行带有 eslint props 验证的 React 功能组件 - Using Typescript generics for React Functional Components with eslint props validation 如何将渲染道具传递给 React 功能组件中的子级? - How can i pass render props to children in React functional components? 我可以使用 Typescript 输入 React Children 吗? - Can I type React Children using Typescript? 如何将 `this` 的引用传递给使用功能组件的孩子? - How can I pass a reference of `this` to children using functional components? 如何让 `eslint-plugin-react-hooks` 对导出为 `default` 的功能组件进行 lint? - How can I get `eslint-plugin-react-hooks` to lint functional components that are exported as `default`? 如何在功能性 React 组件中使用错误边界? - How can I make use of Error boundaries in functional React components? Typescript - 键入 React 组件时 IntrinsicAttributes 和子类型问题 - Typescript - IntrinsicAttributes and children type issues when typing React components 我们可以在React中将类组件用作功能组件的子代吗? - Can we use class components as children for functional components in React? TypeScript - 我应该如何输入这些 React 组件? - TypeScript - how should I type these React components?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM