I can access this.state outside of fetch,but when trying to access state inside fetch, I get this error:
Uncaught TypeError: Cannot read property 'state' of null
I looked through other posts on StackOverflow, but didn't find a working solution to use the state properties in the fetch call. Please help.
class SignIn extends React.Component {
constructor(props){
super(props);
this.state = {
email: '',
password: ''
}
this.handleValueChange = this.handleValueChange.bind(this);
}
handleValueChange(event) {
const property = event.target.name;
this.setState({[property]: event.target.value});
}
handleLogin(event){
event.preventDefault();
fetch("http://localhost:3001/login.json", {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
email: this.state.email, //this is where the error occurs
password: '123456'
})
})
.then(function(res){return res.json();})
.then(function(data){console.log(data)});
}
render() {
return(
<header>
<NavigationBar />
<div className="form-box">
<form onSubmit={this.handleLogin}>
<h2>Login</h2>
<br />
<div className="row">
<div>
<label>Email</label>
<input type="text" name="email" ref='emailInputField'
onChange={(event) => this.handleValueChange(event)}
required
/>
</div>
</div>
<br />
<div className="row">
<div>
<label>Password</label>
<input type="text" name="password"
onChange={(event) => this.handleValueChange(event)}
required
/>
</div>
</div>
<br />
<div className="btn-submit">
<input type="submit" value="Let's go!" />
</div>
</form>
<button onClick={() => console.log("this.state", this.state)} >Show state</button>
</div>
</header>
)
}
}
NOTE: some code not shown for brevity
Have you tried something like this:
Or just bind your function to your component like suggested in the comments and like you already did with your other function. Arrow functions are a lot smoother though.