Hi,
I have a react app making api news call with axios. I am trying to add a live search feature, to search the data I have already fetched.
Site and components are already built, I just cant figure out how to live search the data… I have looked at tutorials, but usually the entire app is on one component, while mine is broken up in different components. I tried to reverse engineer other people live search code, but still no luck… would love it if someone can help me find a solution.
Search.js file:
const Search = ( {props} ) =>{
const [searchValue, setSearchValue] = useState("");
const handleSearchInputChange =(e) => {
setSearchValue(e.target.value);
}
const resetInputField = () => {
setSearchValue("")
}
const callSearchFunction = (e) => {
e.preventDefault();
props.search(searchValue);
resetInputField();
}
return (
<form className="search">
<input
value={searchValue}
onChange={handleSearchInputChange}
type="text"
placeholder="Search News..."
/>
<i class="fas fa-search" onClick={callSearchFunction} type="submit" value="SEARCH"></i>
</form>
);
}
export default Search;
TopBar.js file
function TopbarContainer() {
const [news, setNews] = useState([]);
useEffect(() => {
getNews3().then(data => setNews(data));/
}, []);
return(
<div className="topbar">
<h2>AI News</h2>
<Search />
<div className="topbar-flex">
{news.map((data, index) => <Topbar key={index} props={data} />)}
</div>
</div>
)
}
export default TopbarContainer
link to site: https://tech-news-for-geeks.netlify.com/
link to gh: https://github.com/ainneo/tech-news-api/tree/master/src