Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Nextjs. After submitting with UpdateParams, the new url is called and a JSON object with the new queried data is returned as expected.
The form updates the two state vars.
However, the products in the all-products view are not updated to reflect the form input.
What do I need to do to to refresh the render to reflect the new data in product?

//all-products.js    
import Link from 'next/link'
import React from 'react';
import { useState } from 'react';

//gets data from local api
async function getData(rank, keyword){
   const res = await fetch(`http://localhost:4000/api?top=${rank}&keyword=${keyword}`);
   return res;
} 

export async function getStaticProps() {
  const rank = 5;
  const keyword = "shorts";   
  const response = await getData(rank, keyword);
   const products = await response.json();
   console.log(products);
   if (!products) {
     return {
       notFound: true,
     }
   }
   return {
     props: {
       products,
     },
   }
 }

export default function AllProducts(stuff) {
  let {products} = stuff; 
  const [rank, setRank] = useState("3");
  const [keyword, setKeyword] = useState("shoes");

  //from form 
  const updateParams = async (e) => {
      e.preventDefault();
      const response= await getData(rank, keyword);
      products = await response.json();
    }
    return (
      <div>
        <input
            type='text'
            placeholder='topRank'
            value={rank}
            onChange={e => setRank(e.target.value)}
        />
            <input
              type="text"
              placeholder='searchTerm'
              value={keyword}
              onChange={e => setKeyword(e.target.value)}
            />

        <button
        type='submit'
        onClick={updateParams}>
        Update Params</button>

      <ul>
        {products.Products.map((product) => {
          return (
          <div key={product.Id}>
            <li>{product.Name}</li>

            <li><img width={300} src={ product.imgUrl } alt="product image" /></li>
          </div>
          
      ) }
      )}
      </ul>
     
      </div>
      )
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
565 views
Welcome To Ask or Share your Answers For Others

1 Answer

getStaticProps is run at build-time so it'll provide the data that's available at that time. To update the UI after the user interacts with the form you should put products into state and update it once new params are submitted and you retrieve the new products.

// all-products.js - removed irrelevant code for simplicity
export default function AllProducts(stuff) {
  const [products, setProducts] = useState(stuff.products);
  //...

  const updateParams = async (e) => {
      e.preventDefault();
      const response = await getData(rank, keyword);
      const newProducts = await response.json();
      setProducts(newProducts);
  }

  return (
    //...
    <ul>
      {products.Products.map((product) => {
        return (
          <div key={product.Id}>
            <li>{product.Name}</li>
            <li><img width={300} src={product.imgUrl} alt="product image" /></li>
          </div>
          
        )
      })}
    </ul>
    //...
  )
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...