I am using Flow for type checking, and I have something like this:
type Props = {
name?: ?string, // this means that "name" is NOT mandatory, and its value can be **null** or **undefined**
};
So, my question is, should I get this linting error? Is this the desired behavior?
I would say this is as expected. Since the prop is not required, you should specify a default value. You can decide to set it to null or undefined by default.
I see. Thank you
I didn't understand what should I do in my case (it's also shows eslint error: propType "isSmall" is not required, but has no corresponding defaultProp declaration. (react/require-default-props)):
type SwitcherProps = {
isChecked: boolean,
action: () => void,
isSmall?: boolean
}
export const Switcher = (props: SwitcherProps) => {
return (
<label className={"switcher ".concat(props.isSmall ? "small" : "")}>
<input type="checkbox" checked={props.isChecked} className="input" />
<span role="presentation" className="slider" onClick={props.action} />
</label>
);
};
@AlexanderNaiden add:
Switcher.defaultProps = {
isSmall: false,
};
The error message is confusing because it's singular instead of plural.
(and... #2063 was posted just 2 hours ago!)
I fixed this issue using the default prop
static defaultProps = {
actions: {
function_name: () => null,
},
}
Most helpful comment
@AlexanderNaiden add: