DEV Community

Discussion on: A Complete Introduction to State For JavaScript Developers

Collapse
surajjadhav profile image
Suraj Jadhav • Edited on

I am not nitpicking anything here but we can also create a setState function that would have taken an object as parameter containing keys of the state that need to be changed in the old state. The setState function would then spread this.state object and copy the changed keys into the new state. And at the end of setState, we would call render function.

Something like this

setState: function(newState) {
    this.state = {
        ...this.state, // old state
        ...newState
    };
    this.render();
}
Collapse
reedbarger profile image
Reed Barger Author

I agree, that would be a valuable function to make if we were building out this example into an entire app or attempting to make an app with plain JavaScript in general. Thanks, Suraj.

Collapse
surajjadhav profile image
Suraj Jadhav

That completely makes sense to me. Really loved the way you explained the concept of state in this post. Looking forward to your future posts.