1
- import React , { useState } from 'react' ;
1
+ import React , { useState , useEffect } from 'react' ;
2
2
import logo from '../assets/images/logo.png' ;
3
3
import { Link , useNavigate } from 'react-router-dom' ; // Import Link and useNavigate from react-router-dom
4
4
@@ -11,15 +11,13 @@ function Navbar({ loggedin }) {
11
11
setSearchTerm ( event . target . value ) ;
12
12
} ;
13
13
14
- // Handle search submission
15
- const handleSearchSubmit = ( event ) => {
16
- event . preventDefault ( ) ; // Prevent page refresh
14
+ // Use effect to trigger navigation whenever the search term changes
15
+ useEffect ( ( ) => {
17
16
if ( searchTerm ) {
18
- // Redirect to the search results page with the search term
17
+ // Navigate to search page as the user types
19
18
navigate ( `/search?query=${ encodeURIComponent ( searchTerm ) } ` ) ;
20
- // setSearchTerm(''); // Clear the input after submission
21
19
}
22
- } ;
20
+ } , [ searchTerm , navigate ] ) ; // Trigger effect when searchTerm or navigate changes
23
21
24
22
return (
25
23
< nav className = "navbar navbar-expand-lg" >
@@ -54,29 +52,26 @@ function Navbar({ loggedin }) {
54
52
</ li >
55
53
</ ul >
56
54
< div className = "d-flex align-items-center flex-column flex-lg-row" >
57
- < form className = "me-2 mb-2 mb-lg-0" onSubmit = { handleSearchSubmit } >
58
- < div class = "input-group" >
59
- < span class = "input-group-text" id = "basic-addon1" >
60
- < svg xmlns = "http://www.w3.org/2000/svg" width = "16" height = "16" fill = "currentColor" class = "bi bi-search" viewBox = "0 0 16 16" >
55
+ < div className = "me-2 mb-2 mb-lg-0" >
56
+ < div className = "input-group" >
57
+ < span className = "input-group-text" id = "basic-addon1" >
58
+ < svg xmlns = "http://www.w3.org/2000/svg" width = "16" height = "16" fill = "currentColor" className = "bi bi-search" viewBox = "0 0 16 16" >
61
59
< path d = "M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001q.044.06.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1 1 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0" > </ path >
62
60
</ svg >
63
61
</ span >
64
- { /* <input type="text" class="form-control" placeholder="Input group example" /> */ }
65
-
66
- < input
67
- type = "text"
68
- className = "form-control"
69
- // aria-label="Input group example"
70
- // aria-describedby="basic-addon1"
71
- placeholder = "Search"
72
- value = { searchTerm }
73
- onChange = { handleSearchChange }
74
- /> </ div >
75
- </ form >
62
+ < input
63
+ type = "text"
64
+ className = "form-control"
65
+ placeholder = "Search"
66
+ value = { searchTerm }
67
+ onChange = { handleSearchChange }
68
+ />
69
+ </ div >
70
+ </ div >
76
71
{ loggedin === 'true' ? (
77
- < Link className = "btn btn-primary clk" to = "/profile" > Profile </ Link >
72
+ < Link className = "btn btn-primary clk" to = "/profile" > Profile</ Link >
78
73
) : (
79
- < Link className = "btn btn-primary clk" to = "/login" > Sign up </ Link >
74
+ < Link className = "btn btn-primary clk" to = "/login" > Sign up</ Link >
80
75
) }
81
76
</ div >
82
77
</ div >
0 commit comments