If I go to /products/ and add in the end anything, It will Search from all the products, but when I try to press the search button, it doesnt do anything, not even go to /products/.
I dont know if I should use Button instead of input, I'm following a tutorial and it works there
const Search = ({ history }) => {
const [keyword, setKeyword] = useState("");
const searchSubmitHandler = (e) => {
e.preventDefault();
if (keyword.trim()) {
history.push(`/products/${keyword}`);
} else{
history.push("/products");
}
};
return (
<Fragment>
<form className="searchBox" onSubmit={searchSubmitHandler}>
<input
type="text"
placeholder="Search a Product ..."
onChange={(e) => setKeyword(e.target.value)}
/>
<input type="submit" value="Search" />
</form>
</Fragment>
);
};
export default Search here
Try canceling e.preventDefault ();