JavaScript
x
23
23
1
function Input() {
2
const [input, setInput] = useState("");
3
function handleSearch() {
4
let url = "https://google.com/search?q=${input}"
5
window.open(url)
6
}
7
return (
8
<div className="input-wrap">
9
<input
10
type="text"
11
className="input__search"
12
placeholder="Enter your search..."
13
value={input}
14
onChange={(e) => setInput(e.target.value)}></input>
15
<button
16
className="input__search--btn"
17
onClick={handleSearch}>
18
<i className="fa-solid fa-magnifying-glass"></i>
19
</button>
20
</div>
21
);
22
}
23
The search button when clicked will redirect you to a google search based on the value from the input field, below is the site for advanced search, when active the link will add an additional link after "https://google.com/search?q=${input}+site%3A${activepage}.com
, how do I check if one or many sites are active then pass down its name to url
P/s: code for toggling websites
JavaScript
1
15
15
1
function WebButton({ icon, name }) {
2
const [active, setActive] = useState(false);
3
function handleToggle() {
4
setActive(!active);
5
}
6
return (
7
<button
8
className={active ? "websites-btn active" : "websites-btn"}
9
onClick={handleToggle}>
10
<i className={icon}></i>
11
<div className="websites-name">{name}</div>
12
</button>
13
);
14
}
15
Advertisement
Answer
You can keep a root level state to gather active links to a state. And pass it to the Input
component.
- Update your
Input
component to accept array called “ and update thehandleSearch
to useOR
operation in google search.
JavaScript
1
29
29
1
function Input({ activeLinks }) {
2
const [input, setInput] = useState("");
3
function handleSearch() {
4
if (activeLinks.length > 0) {
5
let compundSearchURL = `https://google.com/search?q=${input}`;
6
activeLinks.forEach((link, i) => {
7
compundSearchURL += `+${i > 0 ? "OR+" : ""}site%3A${link}.com`;
8
});
9
window.open(compundSearchURL);
10
} else {
11
window.open(`https://google.com/search?q=${input}`);
12
}
13
}
14
return (
15
<div className="input-wrap">
16
<input
17
type="text"
18
className="input__search"
19
placeholder="Enter your search..."
20
value={input}
21
onChange={(e) => setInput(e.target.value)}
22
></input>
23
<button className="input__search--btn" onClick={handleSearch}>
24
<i className="fa-solid fa-magnifying-glass">Search</i>
25
</button>
26
</div>
27
);
28
}
29
- Accept another function in
WebButton
calledtoggleActiveLink
and a string calledvalue
which refers to the URL part. Call the function with the value insidehandleToggle
function.
JavaScript
1
18
18
1
function WebButton({ icon, name, toggleActiveLink, value }) {
2
const [active, setActive] = useState(false);
3
function handleToggle() {
4
setActive(!active);
5
toggleActiveLink(value);
6
}
7
return (
8
<button
9
className={active ? "websites-btn active" : "websites-btn"}
10
style={{ color: active ? "blue" : "unset" }}
11
onClick={handleToggle}
12
>
13
<i className={icon}></i>
14
<div className="websites-name">{name}</div>
15
</button>
16
);
17
}
18
- In the main component you have to create a local state to handle the active links. Create the toggle function as given. It will add the value if it is not there otherwise remove it.
JavaScript
1
35
35
1
const urls = [
2
{ name: "Reddit", value: "reddit" },
3
{ name: "Quora", value: "quara" },
4
{ name: "Facebook", value: "facebook" },
5
{ name: "Stackoverflow", value: "stackoverflow" },
6
{ name: "Twitter", value: "twitter" }
7
];
8
9
function App() {
10
const [activeLinks, setActiveLinks] = useState([]);
11
12
const toggleActiveLink = (link) => {
13
const index = activeLinks.indexOf(link);
14
if (index < 0) {
15
setActiveLinks((prevLinks) => [prevLinks, link]);
16
} else {
17
setActiveLinks((prevLinks) => prevLinks.filter((l) => l !== link));
18
}
19
};
20
return (
21
<>
22
<Input activeLinks={activeLinks} />
23
<div>
24
{urls.map(({ name, value }) => (
25
<WebButton
26
name={name}
27
value={value}
28
toggleActiveLink={toggleActiveLink}
29
/>
30
))}
31
</div>
32
</>
33
);
34
}
35