Skip to content

Mutation is a debuggers worst nightmare #17

Description

@akinsho

Here you are very close to what looks like a contravention of sacred law, this sacred law in the land of react (think of it as the first commandment) is Never mutate state 😉 or props you should always be v. wary of code that looks like

	thing1 = "apple"
	thing1 = randomOtherThing

// More scary is
	this.state.apple = "pear"

The reason mutation in this (and many other scenarios) is a big problem is that by reassigning state or mutating state you immediately lose the ability to track what things were. think of it a little like git history if you go back to a previous commit and say delete it or change what happened then your git history becomes hard to follow since there are missing bits, it becomes unclear what happened or how you even got there.

In the case above since what youre trying to do is increment the score by 10 and reassign current score to old score id suggest (you dont actually seem to be setting the oldscore in setState but relying on mutation but you create an new variable when you destructure so nothing happens?)

const { oldScore, score } = this.state
this.setState({ oldScore: score, score: score + 10 })

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions