Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

propType [name] is not required, but has no corresponding defaultProps declaration ReactJS

Tags:

reactjs

import styled from 'styled-components'
import propTypes from 'prop-types'
import React from 'react'

const Checkbox = ({ className, checked, ...props }) => (
  <CheckboxContainer className={className}>
    <HiddenCheckbox checked={checked} {...props} />
    <StyledCheckbox checked={checked}>
      <Icon viewBox="0 0 24 24">
        <polyline points="20 6 9 17 4 12" />
      </Icon>
    </StyledCheckbox>
  </CheckboxContainer>
)
    
Checkbox.propTypes = {
  checked: propTypes.boolean,
  className: propTypes.string,
}

I have this component and I'm always getting this error: error propType "className" is not required, but has no corresponding defaultProps declaration react/require-default-props

I don't know how to fix it any suggestions, please?

like image 869
Nika Roffy Avatar asked Sep 22 '20 14:09

Nika Roffy


People also ask

How do I turn off default props in react?

Use Hello. defaultProps = {} or turn off the rule react/require-default-props ?

Is defaultProps deprecated?

As per this tweet, defaultProps will eventually be deprecated.

What is default props in react?

The defaultProps is a React component property that allows you to set default values for the props argument. If the prop property is passed, it will be changed. The defaultProps can be defined as a property on the component class itself, to set the default props for the class.

Is missing in props validation ESLint disable?

To fix ESLint error missing in props validation with React, we can add a comment or disable the rule globally with a config. to disable prop type check for the line immediately below the comment in our code. in . eslintrc to disable the prop type validation rule for the whole project.


1 Answers

It's explained in the documentation: https://reactjs.org/docs/typechecking-with-proptypes.html#default-prop-values

As you are marking this prop as not required it is asking you to put a default in case the prop is missing.

So, something like this (but default values depends on you):

Checkbox.defaultProps = {
  checked: false,
  className: null,
}
like image 70
keul Avatar answered Sep 19 '22 21:09

keul