I new to react and I'm having a hard time on how to change the title when I click the button.any help would be really appreciated.
import React, { Component } from 'react';
import './style.css';
class Layout extends Component {
constructor() {
super();
this.state = {
name:"Title",
};
}
func() {
this.setState({name: "NewTitle"});
}
render() {
// setTimeout(()=>{
// this.setState({name: "John"});
// },2000)
return (
<div className = "layout">
<h2>{this.state.name}</h2>
<p>{this.props.val}</p>
<input onClick = {this.func} type = "submit" value = "Done"/><br/>
</div>
);
}
}
funcmethod, put this line in the constructor:this.func=this.func.bind(this), Check this fiddle.