Here are 3 basic components searchBar
which refers to the search bar form and, the searchPage
component which displays the search results , and of course, the app
component which contains them all .
mechanism:
handleSubmit
function gets fired, which changes the state of setSearchedProducts
to the input value, by useContext
AND getting pushed to the ("/SearchPage") by history.push()
. import {useState, useContext } from "react"; import { useHistory } from "react-router-dom"; import { LocaleContext } from "../../../LocaleContext"; const SearchBar = () => { const history = useHistory(); const {setSearchedTerm} = useContext(LocaleContext); const handleSubmit = (SearchTerm) => { setSearchedProducts(SearchTerm) history.push("/SearchPage"); } return ( <form> <input onSubmit={(e) => handleSubmit(e.target.value)}> </input> <button type="submit">Submit</button> </form> ) } export default SearchBar
import { useState, useMemo } from "react"; import { searchBar, searchPage } from "./components"; import { BrowserRouter as Router, Switch, Route } from "react-router-dom"; import {LocaleContext} from "./LocaleContext" const App = () => { const [searchedTerm, setSearchedTerm] = useState(""); const providerValue = useMemo(() => ({searchedTerm, setSearchedTerm}), [searchedTerm, setSearchedTerm]) return ( <Router> <LocaleContext.Provider value={providerValue}> <searchBar /> <Switch> <Route exact path="/SearchPage"> <SearchPage /> </Route> </Switch> </LocaleContext.Provider> </Router> ); } export default (App);
useContext
, and with useEffect
, the fetchProducts()
function gets fired, that fetches a set of products based on the state value. import {useState, useEffect, useContext} from 'react'; import { LocaleContext } from "../../LocaleContext"; const SearchPage = ({}) => { const [products, setProducts] = useState([]); const {searchedTerm} = useContext(LocaleContext); const fetchProducts = (term) => { setLoading(true); const url = new URL( "https://example/products" ); let params = { "query": term }; Object.keys(params).forEach(key => url.searchParams.append(key, params[key])); let headers = { "Accept": "application/json", "Content-Type": "application/json", }; fetch(url, { method: "GET", headers: headers, }).then(response => response.json()).then(json => { setProducts(json); }); } useEffect(() => { fetchProducts(searchedProducts) }, []) return ( { products.map(product => ( <div> {product.name} </div> )) } ) } export default SearchPage
Issues:
import {createContext} from "react"; export const LocaleContext = createContext(null);
this is the localeContext
component if needed.
you have to add e.preventDefault()
in your onSubmit
handler. Otherwise you're getting a page reload which causes a state loss.
I noticed "setSearchedProducts" & "setSearchedTerm" should be the same in your code below. This might be your issue!
const SearchBar = () => {... const {setSearchedTerm} = useContext(LocaleContext); const handleSubmit = (SearchTerm) => { setSearchedProducts(SearchTerm)... }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.