A small problem about this.state and this.setState in React

constructor(props) {
    super(props);
    this.state = this.getTime();
}
setTimer() {
    clearTimeout(this.timeout);
    this.timeout = setTimeout(this.updateClock.bind(this), 1000);
}

updateClock() {
    this.setState(this.getTime, this.setTimer);
}

it is a little hard to understand when someone sets the update state, in this way. Can anyone click on it?

Mar.31,2021

take a look at setState's function signature.

The first argument to

setState can be a partialState or a function, or a function that accepts previousState if it is a function.

The second parameter of

setState is callback

this.getTime you don't write a definition, but it must be a function that accepts state. Maybe so?

const getTime = state => Object.assign(...state, ...{a: 1}) //

so your code means * pass the current state to getTime to process the returned object as a new state,
and then call setTime *

Menu