【问题标题】:Fetching data not showing in frontend获取数据未显示在前端
【发布时间】:2022-11-23 20:53:03
【问题描述】:

我正在尝试从后端获取数据到前端,数据获取正常,并显示在控制台日志中,但未显示在前端

编码:

export default function ShopPage() {

  const [disable, setDisable] = useState(false);
  
  const [shop, setShop] = useState("");
  const shopName = useParams().shopName;

  const [shopCoupons, setCoupons] = useState([]);

  const { search } = useLocation();

  useEffect(() => {
    const fetchShop = async () => {
      const res = await axios.get(`/shops/${shopName}`);
      setShop(res.data);
      console.log(res.data);
    };
    fetchShop();
  }, [shopName]);
  
  
  useEffect(() => {
    const fetchShopCoupons = async () => {
      const response = await axios.get(`/coupons/${shopName}`);
      setCoupons(response.data);
      console.log("Shop Coupons are:", response.data);
    
    };
    fetchShopCoupons();
  }, [shopName]);
  
  return (
    <>
        <Box>
          <Stack>
        <Box >

        {shopCoupons.map(c => (
            
            <Coupon coupon={c} />
            
        ))}

        </Box>
        </Stack>
        </Box>
        
    </>
  )
}

我认为问题出在shopCoupons.map 我不知道我在这里遗漏了什么

【问题讨论】:

  • 您正在使用 useEffect 两次,我很确定它不能以这种方式工作...
  • 你的问题不清楚。什么没有显示?你得到任何错误或者你什么都不渲染?
  • 你的 Coupon 组件是什么样的?
  • 你能告诉我们优惠券组件吗?

标签: javascript reactjs fetch fetch-api


【解决方案1】:

您可以尝试添加密钥和吗?像优惠券组件

shopCoupons?.map((c, index) =>
 
  <Coupon coupon={c} key={index} />
);

【讨论】:

  • 即使缺少关键道具会发出警告,它也不会阻止渲染
猜你喜欢
  • 2018-05-29
  • 1970-01-01
  • 2021-12-03
  • 2018-11-26
  • 2022-12-19
  • 2020-07-18
  • 2019-10-04
  • 2016-11-24
  • 2021-04-01
相关资源
最近更新 更多