出现状况: 明明改变了值, 而且回调函数也触发了, 可是就是不触发renderreact
import React, { PureComponent } from 'react' import { InputNumber } from 'antd' export default class example extends PureComponent{ //... state = { fruit: [{ type: 'bannana', count: 0 },{ type: 'apple', count: 0 }] } handleChange(val, type){ let { fruit } = this.state; fruit.map(item => { if(item.type == type) item.count = val }) this.setState({ fruit, },() => { console.log('触发回调函数') }) } render(){ console.log('触发render') return( <InputNumber onChange={(val) => this.handleChange(val,'apple')} /> ) } }
出现了浅比较, 不触发render
生命周期antd
解决方法: 赋值的时候改变fruit
的指向.app
this.setState({ fruit: [...fruit] },() => { console.log('触发回调函数') })