I'm newbie to React JS. I'm trying to workout setState sample program to change the text while clicking the button using below code.
import React, {Component} from 'react'
class StateExample extends Component {
constructor(){
super()
this.state = {
message : 'state example'
}
}
changeMessage() {
alert("I was clicked");
console.log(this);
this.setState = ({
message : 'changed to hai'
})
}
/* handleEvent = event => {
alert("I was clicked");
}; */
render(){
return (
<div>
<h2> {this.state.message} </h2>
<button onClick={() => this.changeMessage()}>Click on me</button>
</div>
)
}
}
export default StateExample
No error or warning occurs.alert is working while clicking,but text is not changing.
Console is returning below things which doesn't having setState. Is this the reason?
[object Object]: {_reactInternalFiber: Object, _reactInternalInstance: Object, context: Object, isMounted: undefined, props: Object...}
_reactInternalFiber: Object
_reactInternalInstance: Object
context: Object
isMounted: undefined
props: Object
refs: Object
replaceState: undefined
state: Object
updater: Object
__proto__: Object
Notify my mistake.Appreciate your help!!