react - ts中,render内不允许匿名函数,如果不使用匿名函数,则提示不允许更新state

const renderItem = (item: string, index: number) => {
  console.log(warpProps.handleItemDelete)
  return (
    <List.Item>
      <p onClick={warpProps.handleItemDelete(index)}>{item}</p>
    </List.Item>
  )
}

报错

Warning: Cannot update during an existing state transition (such as within `render`). Render methods should be a pure function of props and state.

如果是匿名函数

const renderItem = (item: string, index: number) => {
  console.log(warpProps.handleItemDelete)
  return (
    <List.Item>
      <p onClick={() => warpProps.handleItemDelete(index)}>{item}</p>
    </List.Item>
  )
}

报错

Lambdas are forbidden in JSX attributes due to their rendering performance impact
阅读 4.7k
2 个回答

报错是tslint报错,我把匿名函数限制关了就好了

新手上路,请多包涵

直接

<div onClick={this.onChange}></div> 

就可以了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题