Skip to content
Advertisement

How to change text on mouse event in React

I’m trying to use the onMouseDown event in react to change the text but it doesn’t work. I saw a similar code which actually worked so I have no idea what could be wrong.

JavaScript

Console says

Uncaught TypeError: this.setState2 is not a function

The first state which changes images on button click actually works but I’ve pasted the whole code since maybe there is some interaction.

Advertisement

Answer

You should use only one react’s state in Class Component, in that way can access the setState and Update it. A state is an object so it can contain inside of it more variables, arrays, or even more objects. just hold inside the same state, variable for tekst

and update it like you update the first state, like so:

JavaScript

And then update the state whenever you need, like so:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement