I am having a weird bug: After submitting a search form, my code redirects to the listing page and renders the products based on the search input.
This works on every site, except if i am already on the listing page. So i thought about redirecting first to the homepage, and after that redirecting to listing page, if i am already on /listing.
Unfortunatelly, using history.push if i am already on listing does not change anything. Only the second history.push() gets activated. Where could be my mistake?
(Even if i delete the if request, it does not redirect to "/". However, if i delete the second history.push, it does)
const SearchInput = ({history}) => {
const [searchword, setSearchword] = useState('');
const submitHandler = (e) => {
e.preventDefault();
console.log(history.location.pathname)
if (history.location.pathname==="/listing"){
history.push({
pathname: HOME_PAGE,
search: '?'+searchword,
state: { detail: 'some_value' }
});
}
history.push({
pathname: LISTING_POSTS_PAGE,
search: '?'+searchword,
state: { detail: 'some_value' }
//
})
}
return (
<form className="search" onSubmit={submitHandler}>
<div className = "row">
<input
type = "text"
searchword = "q"
id = "q"
placeholder = "What do you want to buy?"
onChange = {(e) => setSearchword(e.target.value)}>
</input>
</div>
</form>
);
};