Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

182
Visualizações
Why my search result page can fetch the input words?

I'm writing the search result page now and I want to use dynamic route to implement. This is my search bar code,

<Link href={`/productSearchResult/${searchWord}`}>
  <a className={styles.navbar_searchbar_dataResult_productItem}>
    <p>{filteredData}</p>
  </a>
</Link>

searchWord is the user's input, and if user click the autocompleted link, it will jump into the searchResult page. This is my search result code,

import Navbar from "../../components/navbar"
import Head from "next/dist/shared/lib/head"
import searchResult from "../../components/searchResult"
import productList from "../../components/productList"

export default function productionSearchResult({ searchName }) {

  const [searchedProducts, setSearchedProducts] = useState([])
  console.log(searchName);
  const productsInformation = searchResult(searchName);
  productsInformation.then(function(result) {
    setSearchedProducts(result.props)
  })
  
  return (
    <div>
      <Head>
        <title>Growcery | Search Result</title>
        <meta name="description" content="Your search result | Growcery" />
        <link rel="icon" href="/favicon.ico" />
      </Head>
      <Navbar/>
      <main>
        <p>Showing results for {searchName}</p>
        <productList products={searchedProducts} />
      </main>
    </div>
  )
}

export async function getServerSideProps({ product_name }) {
  const productName = product_name;
  const res = await fetch(`${process.env.NEXT_PUBLIC_API_LINK}search-products/${productName}/`);
  let data = "";
  if (res)
    data = await res.json();

  return {
    props: {
      products: data.data || "",
    }
  }
}

I wrote a console.log(searchName) to see if the code can fetch the searchName, here is the screenshot of my google Chrome. route path console.log(searchName) in google chrome

But I can only see undefined in my chrome terminal.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

  1. There is an easy way catch query parameter and use it any where in component.
  2. It also depend on your routing for this your route must be like.
       <Route exact path="/productSearchResult/:name?"></Route>
    ------------------------------------------------------------------------
      import Navbar from "../../components/navbar"
    import Head from "next/dist/shared/lib/head"
    import searchResult from "../../components/searchResult"
    import productList from "../../components/productList"
    
    export default function productionSearchResult({props}) {
    
      const [searchedProducts, setSearchedProducts] = useState([])
       let searchName = props.match.params.name;
      console.log(searchName);
      const productsInformation = searchResult(searchName);
      productsInformation.then(function(result) {
        setSearchedProducts(result.props)
      })
      
      return (
        <div>
          <Head>
            <title>Growcery | Search Result</title>
            <meta name="description" content="Your search result | Growcery" />
            <link rel="icon" href="/favicon.ico" />
          </Head>
          <Navbar/>
          <main>
            <p>Showing results for {searchName}</p>
            <productList products={searchedProducts} />
          </main>
        </div>
      )
    }
    
    export async function getServerSideProps({ product_name }) {
      const productName = product_name;
      const res = await fetch(`${process.env.NEXT_PUBLIC_API_LINK}search-products/${productName}/`);
      let data = "";
      if (res)
        data = await res.json();
    
      return {
        props: {
          products: data.data || "",
        }
      }
    }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda