eslint-plugin-react: button-has-type is not allowing a ternary statement
I’m trying to add a ternary statement to dynamically determine which type to apply to this button component. According to https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/button-has-type.md, this should work?
The error message:
The button type attribute must be a static string
My code:
import React from 'react';
import {
string,
bool,
arrayOf,
node,
func,
oneOf,
oneOfType,
} from 'prop-types';
import styles from './StyledButton.module.css';
const StyledButton = ({ children, size, type, colorScheme, ...otherProps }) => {
const assignClassName = () => {
const style = [styles.button];
if (colorScheme) {
style.push(styles[colorScheme]);
}
if (size) {
style.push(styles[size]);
}
return style.join(' ');
};
const test = false;
return (
<button
className={assignClassName(colorScheme, size)}
type={test ? 'submit' : 'button'} // <================ why doesn't this work?
{...otherProps}
>
{children}
</button>
);
};
StyledButton.propTypes = {
children: oneOfType([arrayOf(node), node]).isRequired,
onClick: func.isRequired,
size: string.isRequired,
type: bool.isRequired,
colorScheme: oneOf(['default', 'bright', 'vivid']).isRequired,
};
export default StyledButton;

About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 16 (9 by maintainers)
@ljharb Yes it does… I had updated to the newest version and I didn’t think to restart my vscode. 🤦
Thank you for all the help regardless!
@ljharb please see below.
Indeed https://github.com/yannickcr/eslint-plugin-react/commit/d8741de74da0fb7a0cb730f0fea14de05e4faa9b should be in v7.21.0+ via #2748.
(cc @Hypnosphi)
This seems like a bug.