Hello everyone, I am stuck on my personal project and am hoping that this will only take 5 minutes of someone’s time.
I am trying to create an input where you can enter your name and it will display “hi (name)”
However when I tried the whole app crashed with
TypeError: Cannot read property ‘setState’ of undefined
class NameInput extends Component {
state = { personsname: null }
render() {
return (
<div>
<h1>Hello, please enter your name.</h1>
<input id="personsname">Your name here</input>
<button id="namebutton" onClick={setname()} >Enter</button>
<h1> Hi! {this.state.personsname}</h1>
</div> );
}
}
function setname (){
this.setState.personsname = document.getElementById('namebutton')
}
I know something is wrong (probably in the state) but I can’t seem to figure it out.
Thanks in advance for your help.