出現狀況: 明明改變了值, 而且回調函數也觸發了, 可是就是不觸發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('觸發回調函數') })