Advertisement
shadiff

post-it search

Mar 31st, 2023
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. import { useState } from 'react';
  2. import { useNavigate } from 'react-router-dom';
  3. import axios from 'axios';
  4. import './search.css'
  5.  
  6. function SearchBar() {
  7. const [query, setQuery] = useState('');
  8. const [showSearchBar, setShowSearchBar] = useState(false);
  9.  
  10. const toggleSearchBar = () => {
  11. setShowSearchBar(!showSearchBar);
  12. };
  13.  
  14. const navigate = useNavigate();
  15.  
  16. const handleSearch = async (e) => {
  17. e.preventDefault();
  18. const response = await axios.get(`https://api.example.com/search?q=${query}`);
  19. navigate('/results', { state: { results: response.data } });
  20. };
  21.  
  22. return (
  23. <form onSubmit={handleSearch}>
  24. <div className="search-bar-container">
  25. <div onClick={toggleSearchBar}>
  26. <ion-icon class="search" name="search-outline"></ion-icon>
  27.  
  28. </div>
  29. {showSearchBar && (
  30. <div className="search-bar">
  31. <input type="text"
  32. value={query}
  33. onChange={(e) => setQuery(e.target.value)}
  34. placeholder="Search..." />
  35. <button type="submit">Search</button>
  36. </div>
  37. )}
  38. </div>
  39.  
  40. </form>
  41.  
  42. );
  43. }
  44.  
  45. export default SearchBar;
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement