강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

hou1201님의 프로필 이미지
hou1201

작성한 질문수

비전공자를 위한 진짜 입문 올인원 개발 부트캠프

상품 상세 페이지 구현 - 2

상품 상세 화면이 나오지 않아요..

작성

·

146

1

상품 상세 화면에서 비동기식 처리로 null 이 끝나고 다음 처리가 되지 않는 것 같아요

이 화면에서 새로고침을 하거나 그냥 기다려도 변하지 않네요 뭐가 문제인것일까요?????

product 의 index.js 함께 첨부합니다.

import { useParams } from "react-router-dom";
import axios from "axios";
import { useEffect, useState } from "react";

function ProductPage() {
  const { id } = useParams();
  const [product, setProduct] = useState(null);
  useEffect(function () {
    axios
      .get(
        "https://a961ad84-b349-455c-9581-09eecba1d299.mock.pstmn.io/products" +
          id
      )
      .then(function (result) {
        setProduct(result.data);
      })
      .catch(function (error) {
        console.error(error);
      });
  }, []);

  if (product === null{
    return <h1>상품 정보를 받고 있습니다..</h1>;
  }

  return (
    <div>
      <div id="image-box">
        <img src={"/" + product.imageUrl/>
      </div>
      <div id="profile-box">
        <img src="/images/images/icons/avatar.png" />
        <span>{product.seller}</span>
      </div>
      <div id="contents-box">
        <div id="name">{product.name}</div>
        <div id="price">{product.price}</div>
        <div id="description">{product.description}</div>
      </div>
    </div>
  );
}

export default ProductPage;

답변 1

0

그랩님의 프로필 이미지
그랩
지식공유자

axios.get 코드를 보면 ... product" + id 로 되어있는데 product/" + id로 변경해보시겠어요?!

hou1201님의 프로필 이미지
hou1201

작성한 질문수

질문하기