I'm building a simple user search app using React and TypeScript. I have a basic form with an input text box to search users and an input button that submits the search. But the e.preventDefault()
method in my onSubmit
method doesn't seem to be working. When I submit, the whole app refreshes. Actually, it may be that onSubmit
isn't even being called at all.
private handleSubmit = (e: React.FormEvent<HTMLInputElement>) => {
e.preventDefault();
this.props.searchUsers(this.state.text);
this.setState({text: ''});
}
private handleChange = (e: React.FormEvent<HTMLInputElement>) => {
this.setState({text: e.currentTarget.value});
}
public render() {
return (
<div>
<form className="form">
<input
type="text"
name="text"
value={this.state.text}
placeholder="Search Users..."
onChange={this.handleChange}
/>
<input
type="submit"
value="search"
className="btn btn-dark btn-block"
onSubmit={this.handleSubmit}
/>
</form>
</div>
);
}
Any suggestions? Thanks!