簡體   English   中英

Eslint:功能組件中的默認道具問題(Typescript - React)

[英]Eslint: Problem with default props in functional component (Typescript - React)

我擁有的

import { NextPage } from 'next';
import React from 'react';

interface Props {
  name: string;
  gretting?: string; // Error: ESLint: propType "gretting" is not required, but has no corresponding defaultProps declaration.(react/require-default-props) 
}

const Hello: React.FunctionComponent<Props> = ({ name, gretting = 'night' }: Props) =>
  <p>Hi {name} Good {gretting}</p>;

const Home: NextPage = () => <Hello name="Jhon Doe" />;

export default Home;

問題

Eslint react 插件抱怨這個錯誤ESLint: propType "gretting" is not required, but has no corresponding defaultProps declaration.(react/require-default-props)

根據這個答案,用於defaultProps和默認參數值的方法很好,那么解決這個問題的最佳方法是什么? 使用Hello.defaultProps = {}或關閉規則react/require-default-props 有更好的方法嗎?

我遇到了這個問題並通過導出類型或接口來修復它,但我無法解釋為什么這是問題的解決方案。

export interface Props {
  name: string;
  greeting?: string;
}

const Hello = ({name, greeting = 'Hi'}: Props): JSX.Element = {}

編輯:根據此答案,發現您需要 Typescript 3.0: https ://stackoverflow.com/a/51486735/5059401

我有的

import { NextPage } from 'next';
import React from 'react';

interface Props {
  name: string;
  gretting?: string; // Error: ESLint: propType "gretting" is not required, but has no corresponding defaultProps declaration.(react/require-default-props) 
}

const Hello: React.FunctionComponent<Props> = ({ name, gretting = 'night' }: Props) =>
  <p>Hi {name} Good {gretting}</p>;

const Home: NextPage = () => <Hello name="Jhon Doe" />;

export default Home;

問題

Eslint react插件抱怨此錯誤ESLint: propType "gretting" is not required, but has no corresponding defaultProps declaration.(react/require-default-props)

根據此答案,使用帶有默認參數值的defaultProps的方法很好,那么解決此問題的最佳方法是什么? 使用Hello.defaultProps = {}或關閉規則react/require-default-props嗎? 有更好的方法嗎?

我有的

import { NextPage } from 'next';
import React from 'react';

interface Props {
  name: string;
  gretting?: string; // Error: ESLint: propType "gretting" is not required, but has no corresponding defaultProps declaration.(react/require-default-props) 
}

const Hello: React.FunctionComponent<Props> = ({ name, gretting = 'night' }: Props) =>
  <p>Hi {name} Good {gretting}</p>;

const Home: NextPage = () => <Hello name="Jhon Doe" />;

export default Home;

問題

Eslint react插件抱怨此錯誤ESLint: propType "gretting" is not required, but has no corresponding defaultProps declaration.(react/require-default-props)

根據此答案,使用帶有默認參數值的defaultProps的方法很好,那么解決此問題的最佳方法是什么? 使用Hello.defaultProps = {}或關閉規則react/require-default-props嗎? 有更好的方法嗎?

在此處輸入圖片說明

這是我的代碼。 我使用“componentName”.defaultProps={ variable:defaultValue, } 來解決問題。希望它會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM