简体   繁体   English

缺少道具验证(道具类型)

[英]Missing in props validation (props-types)

I'm trying to transfer my code below from a project to another project (CoreUI) that has .eslintrc and .prettier rules.我正在尝试将下面的代码从一个项目转移到另一个具有.eslintrc.prettier规则的项目(CoreUI)。 Anyway, doing that (transferring code) gets me a couple of errors that i dont know how to fix them.无论如何,这样做(传输代码)会给我带来一些我不知道如何解决的错误。 Please help.请帮忙。

Note : I found this question but i dont know how to use the solution to fix this issue.注意:我发现了这个问题,但我不知道如何使用该解决方案来解决此问题。


Errors:错误:

  Line 13:11:  'notify' is missing in props validation          react/prop-types
  Line 13:19:  'setNotify' is missing in props validation       react/prop-types
  Line 26:20:  'notify.isOpen' is missing in props validation   react/prop-types
  Line 31:31:  'notify.type' is missing in props validation     react/prop-types
  Line 32:17:  'notify.message' is missing in props validation  react/prop-types

The Code:编码:

import { Snackbar } from '@mui/material'
import React from 'react'
import { Alert } from '@material-ui/lab'
import { makeStyles } from '@material-ui/core'

const useStyles = makeStyles((theme) => ({
  root: {
    top: theme.spacing(8),
  },
}))

export default function Notification(props) {
  const { notify, setNotify } = props
  const classes = useStyles()

  const handleClose = (event, reason) => {
    setNotify({
      ...notify,
      isOpen: false,
    })
  }

  return (
    <Snackbar
      className={classes.root}
      open={notify.isOpen}
      autoHideDuration={3000}
      anchorOrigin={{ vertical: 'top', horizontal: 'right' }}
      onClose={handleClose}
    >
      <Alert severity={notify.type} onClose={handleClose}>
        {notify.message}
      </Alert>
    </Snackbar>
  )
}

.eslintrc.js Code: .eslintrc.js 代码:

module.exports = {
  // parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaVersion: 2020,
    sourceType: 'module',
    ecmaFeatures: {
      jsx: true,
    },
  },
  settings: {
    react: {
      version: 'detect',
    },
  },
  extends: [
    'react-app',
    'react-app/jest',
    'plugin:react/recommended',
    'plugin:prettier/recommended',
  ],
  plugins: ['react', 'react-hooks'],
  rules: {  },
}

In your.eslintrc file, add a rule to disable the props type validation在您的 .eslintrc 文件中,添加一条规则以禁用道具类型验证

rules: { "react/prop-types": 0 }

Or add prop type validation for your component或者为您的组件添加道具类型验证

import PropTypes from 'prop-types';

//...
Notification.propTypes = {
    notify: PropTypes.shape({
        message: PropTypes.string,
        type: PropTypes.string,
        isOpen: PropTypes.bool
    }),
    setNotify: PropTypes.func
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM