misty0304 / day-interview

前端每日一题,提倡每日学习与思考,每天进步一点!
13 stars 1 forks source link

【react】传入 setState 函数的第二个参数的作用是什么? #118

Open misty0304 opened 4 years ago

misty0304 commented 4 years ago

该函数会在setState函数调用完成并且组件开始重渲染的时候被调用,我们可以用该函数来监听渲染是否完成:

this.setState(
  { username: 'tylermcginnis33' },
  () => console.log('setState has finished and the component has re-rendered.')
)
this.setState((prevState, props) => {
  return {
    streak: prevState.streak + props.count
  }
})