I am learning typescript and was trying to implement a detailList component from Microsoft Fluent UI, but I ran into some trouble:
Type '(ev: React.MouseEvent<HTMLElement>, checked: boolean) => void' is not assignable to type '(event: MouseEvent<HTMLElement, MouseEvent>, checked?: boolean | undefined) => void'.
In the code there was a function called _onChangeCompactMode being referenced when a toggle button is clicked
private _onChangeCompactMode = (ev: React.MouseEvent<HTMLElement>, checked: boolean): void => {
this.setState({ isCompactMode: checked });
Here is where it's referenced:
<Fabric>
<div className={classNames.controlWrapper}>
<Toggle
label="Enable compact mode"
checked={isCompactMode}
onChange={this._onChangeCompactMode}
onText="Compact"
offText="Normal"
styles={controlStyles}
/>
...
</Fabric>
I fixed this issue by changing this line onChange={this._onChangeCompactMode}
to this onChange={() => this._onChangeCompactMode}
I am not sure why this made it work. My understanding is, I passed a function reference. But why wouldn't it work as it is without () =>
Still new here, so constructive feedback is welcomed.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…