简体   繁体   中英

setState error for onclick in react

I'm get this error Warning: setState(...): Cannot update during an existing state transition (such as within render or another component's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved to componentWillMount for

<a key={brand} onClick={this.someFn(Brands[brand])}>
 <a key={brand} onClick={() => this.someFn(Brands[brand])}>

用箭头功能调用它

I strongly advise against mutating the state inside render() methods. In fact it is strongly suggested by the maintainers of React, to keep the render() method as a pure function.

If you want to change the state between different components, for example if you have a sidebar, the most robust solution is to use Redux or an equivalent application state library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM