如何将搜索查询附加到React中的搜索页面上的URL?

发布于 2025-01-27 11:28:33 字数 1421 浏览 1 评论 0原文

我的标题搜索栏可通过导航到“搜索?q =”按钮按下按钮,从搜索页面的URL读取参数并处理它们,但是当我从搜索页面搜索时,URL采用此表单“搜索/搜索?q = q = ”。在这种情况下,如何跳过额外的“搜索”?

我尝试了这个解决方案,但它不起作用

import React, { useState } from 'react'
import { useLocation, useNavigate } from 'react-router-dom'
import './style.css'

function SearchBar() {
    const navigate = useNavigate();
    const location = useLocation();
    const [input, setInput] = useState('hi');
    const search = () =>{
        const path = location.pathname
        if(path.includes('search'))
            navigate('?q='+input)
        else
            navigate('search?q=' + input)
    }
    return (
        <form class="form-inline">
            <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search"  onChange={e=>{setInput(e.target.value)}}/>
            <button class="btn btn-outline-success my-2 my-sm-0" type="submit" onClick={search}>
                <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-search" viewBox="0 0 16 16">
                    <path d="M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z" />
                </svg>
            </button>
        </form>
    )
}

export default SearchBar

My header search bar works by navigating to "search?q=" on button press, reading params from the search page's URL and processing them, but when I am searching from the search page the URL takes this form "search/search?q=". How can I skip the extra "search" in this case?

I tried this solution but it didn't work

import React, { useState } from 'react'
import { useLocation, useNavigate } from 'react-router-dom'
import './style.css'

function SearchBar() {
    const navigate = useNavigate();
    const location = useLocation();
    const [input, setInput] = useState('hi');
    const search = () =>{
        const path = location.pathname
        if(path.includes('search'))
            navigate('?q='+input)
        else
            navigate('search?q=' + input)
    }
    return (
        <form class="form-inline">
            <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search"  onChange={e=>{setInput(e.target.value)}}/>
            <button class="btn btn-outline-success my-2 my-sm-0" type="submit" onClick={search}>
                <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-search" viewBox="0 0 16 16">
                    <path d="M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z" />
                </svg>
            </button>
        </form>
    )
}

export default SearchBar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多情癖 2025-02-03 11:28:33

这样更改您的代码。我在表单标签上添加了onsubmit。并在search()中进行了一些更改。

另外,我添加了使用效率来从URL获取参数。

import React, { useEffect, useState } from "react";
import { useLocation, useNavigate, useParams } from "react-router-dom";
import './style.css'

function SearchBar() {
  const navigate = useNavigate();
  const location = useLocation();
  const params = useParams();
  const [input, setInput] = useState("hi");

  const search = (e) => {
    e.preventDefault();

    const path = location.pathname;

    if (path === "/search") {
      navigate("?q=" + input);
    } else {
      navigate("search?q=" + input);
    }
  };

  useEffect(() => {
    const param = new URL(window.location.href).searchParams.get("q");
    if (param) console.log(param);
  }, [params]);

  return (
    <form className="form-inline" onSubmit={search}>
      <input
        className="form-control mr-sm-2"
        type="search"
        placeholder="Search"
        aria-label="Search"
        onChange={(e) => {
          setInput(e.target.value);
        }}
      />
      <button className="btn btn-outline-success my-2 my-sm-0" type="submit">
        <svg
          xmlns="http://www.w3.org/2000/svg"
          width="16"
          height="16"
          fill="currentColor"
          className="bi bi-search"
          viewBox="0 0 16 16"
        >
          <path d="M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z" />
        </svg>
      </button>
    </form>
  );
}

export default SearchBar;




Change your code like this. I've added onSubmit on form tag. And made a little change in search().

Also, I've added a useEffect to get params from url.

import React, { useEffect, useState } from "react";
import { useLocation, useNavigate, useParams } from "react-router-dom";
import './style.css'

function SearchBar() {
  const navigate = useNavigate();
  const location = useLocation();
  const params = useParams();
  const [input, setInput] = useState("hi");

  const search = (e) => {
    e.preventDefault();

    const path = location.pathname;

    if (path === "/search") {
      navigate("?q=" + input);
    } else {
      navigate("search?q=" + input);
    }
  };

  useEffect(() => {
    const param = new URL(window.location.href).searchParams.get("q");
    if (param) console.log(param);
  }, [params]);

  return (
    <form className="form-inline" onSubmit={search}>
      <input
        className="form-control mr-sm-2"
        type="search"
        placeholder="Search"
        aria-label="Search"
        onChange={(e) => {
          setInput(e.target.value);
        }}
      />
      <button className="btn btn-outline-success my-2 my-sm-0" type="submit">
        <svg
          xmlns="http://www.w3.org/2000/svg"
          width="16"
          height="16"
          fill="currentColor"
          className="bi bi-search"
          viewBox="0 0 16 16"
        >
          <path d="M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z" />
        </svg>
      </button>
    </form>
  );
}

export default SearchBar;




~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文