material-UI 事件方法

2021-01-06  本文已影响0人  想溜了的蜗牛

如果 button 的 onClick 事件方法中需要带参数, 那么MUI的写法其实是有点不一样的,如下

<li key={id}>
    <Chip
        label={id}
        onDelete={handleDelete(account)} //这里要传参数
        className={classes.chip}
    />
</li>

再看一下 handleDelete 方法的代码:

const handleDelete = delAccount => () => {  // 看这行
    console.log('handleDelete', displayArr)
    xxContext.dispatch({ type: 'ID_SELECTED', payload: displayArr })
    setDisplayAcc(displayArr);
};

在方法定义时,handleDelete 返回的是一个函数,这里要注意,不这么写 MUI 代码会报错,比如下边这个:
Warning: Cannot update a component (xxxpage) while rendering a different component (yyy). To locate the bad setState() call insideyyy, f ....

上一篇 下一篇

猜你喜欢

热点阅读