Open vnues opened 4 years ago
This bug seems to be reproducible with every reserved words in JS contained in a file name.
I got the same error with a file named default.tsx
Whew, I thought I was the only one! This was tripping me up all day. I'm glad someone realized it was based off reserved words!
Here's my example to help:
Component:
Output:
Error Output:
const prefixCls = getPrefixCls('switch')
export type SwitchSize = 'small' | 'defaut' export type SwitchChangeEventHandler = ( checked: boolean, event: React.MouseEvent,
) => void
export type SwitchClickEventHandler = SwitchChangeEventHandler
export interface SwitchProps extends Omit<React.HTMLAttributes, 'onChange' | 'onClick'> {
/ 开关大小 */
size?: SwitchSize
/* Switch 器类名 /
className?: string
/ 指定当前是否选中 */
checked?: boolean
/ 初始是否选中 */
defaultChecked?: boolean
title?: string
/* 是否禁用 /
disabled?: boolean
/ 变化时回调函数 */
onChange?: SwitchChangeEventHandler
/* 点击时回调函数 /
onClick?: SwitchClickEventHandler
style?: CSSProperties
}
const fixControlledValue = (value: boolean | undefined): boolean => {
if (typeof value === 'undefined' || value === null) {
return false
}
return value
}
export const Switch: FC = (props: SwitchProps) => {
const {
className,
size,
disabled,
checked,
onClick,
onChange,
defaultChecked,
...restProps
} = props
const [checkedState, setCheckState] = useState(fixControlledValue(defaultChecked))
const classes = classNames(prefixCls, className, {
})
useEffect(() => { if (typeof checked !== 'undefined' && checked !== null) { setCheckState(checked) } }, [checked])
const handleClick = (e: React.MouseEvent) => {
setCheckState(!checkedState)
onClick?.(!checked, e)
onChange?.(!checked, e)
}
return (
<button
{...restProps}
type="button"
className={classes}
disabled={disabled}
role="switch"
aria-checked={checkedState}
onClick={handleClick}
Switch.displayName = 'Switch'
Switch.defaultProps = { disabled: false, } export default Switch