question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`button-has-type` not recognizing default props

See original GitHub issue

Hi,

looks like whatever I try the button-has-type rule can’t understand the default prop.

type Props = {
  type: 'submit' | 'button' | 'reset',
};

function Button({ type, ...extraProps }: Props) {
  return <button type={type} {...extraProps} />;
}

Button.defaultProps = {
  type: 'submit',
};
error  "null" is an invalid value for button type attribute  react/button-has-type
type Props = {
  type: 'submit' | 'button' | 'reset',
};

function Button({ type = 'submit', ...extraProps }: Props) {
  return <button type={type} {...extraProps} />;
}
error  "null" is an invalid value for button type attribute  react/button-has-type

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:53
  • Comments:27 (15 by maintainers)

github_iconTop GitHub Comments

23reactions
ljharbcommented, Jul 18, 2018

@truckingsim what i’d expect is that you’d have that Button component wrapped by SubmitButton and ResetButton components, and inside those you’d override the rule for the dynamic type - but you wouldn’t need that anywhere else.

(ofc, this is separate from the issue that reset buttons are terrible UX and shouldn’t ever be on the page)

13reactions
truckingsimcommented, Jul 18, 2018

@ljharb In our component library at my company we have a Button component that is used for various things. We use a oneOf prop type to do an enum of type options and default to button since that’s the sane default. But the button can be used technically as a submit or reset button (and does get used).

We have a component due to css and classes, its easier to have one shared one where the app people don’t have to worry about css classes and semantics, they can just use our Button.

I don’t want to turn this rule off, but if its going to be forced to be static, don’t really see another way.

Read more comments on GitHub >

github_iconTop Results From Across the Web

button-has-type not recognizing default props #1846 - GitHub
Hi, looks like whatever I try the button-has-type rule can't understand the default prop. type Props = { type: 'submit' | 'button' |...
Read more >
Why are React defaultProps not passing values?
If I set default value when destructuring props like: { design: { size='md', color='black', outline, hover }, content, type, onClick } , it ......
Read more >
12 essential ESLint rules for React - LogRocket Blog
react/require-default-props. Depending on the component, some props may be required while others are optional. If an optional prop is not passed ...
Read more >
vue/require-default-prop
Rule Details #. This rule requires default value to be set for each props that are not marked as required (except Boolean props)....
Read more >
eslint-plugin-react - npm
react/default-props-match-prop-types: Enforce all defaultProps are defined and not "required" in propTypes. react/destructuring-assignment: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found