• 로그인
  • 장바구니에 상품이 없습니다.

home2 게시판 JavaScript, TS 게시판 [TS] Axios 를 이용해 res.data를 useState의 저장하는법

[TS] Axios 를 이용해 res.data를 useState의 저장하는법

2 글 보임 - 1 에서 2 까지 (총 2 중에서)
  • 글쓴이
  • #55501

    희라
    참가자
    대분류 셀렉터의 버튼의 Text 값을 axios.get 요청으로 params type 의 value로 보내고,
    응답 데이터를 받으면 그걸 useState의 저장했다가 map으로 뿌려주는 코드를 작성하고 있습니다.
    res.data 를 받아오는 건 성공했는데 useStete의 저장하려고 하니 null 이 뜹니다 ㅜㅜ
    upperText는 버튼의 text 값입니다
    
    const [categorie, setCategorie] = useState(null);
    const data = () => {
    axios
    .get(`/api/products/review-search`, {
     params: { type: upperText },
    })
    .then((res) => {
     console.log(`res.data`, res.data); // => 콘솔에 데이터 잘 찍힙니다
    setCategorie(res.data); 
     console.log(`categorie`, categorie); // => null 이라고 찍힙니다
    })
    .catch(function (error) {
    if (error.response) {
     console.log(`error.response.data`, error.response.data);
     console.log(`error.response.status`, error.response.status);
     console.log(`error.response.headers`, error.response.headers);
    } else if (error.request) {
     console.log(`error.request`, error.request);
    } else {
     console.log('Error', error.message);
    }
     console.log(`error.config`, error.config);
    });
    };
    
    <div onClick={handleSelect} >
    {categorie.name.map((product, idx) => {  //categorie 개체가 null 이라고 오류가 납니다 
    return (
    <button key={idx}>{product.name}</button>
     );
    })}
    </div>
    
    #55547

    codingapple
    키 마스터
    state에 뭐가 들어있으면 map하라고 if문같은거 추가합시다
2 글 보임 - 1 에서 2 까지 (총 2 중에서)
  • 답변은 로그인 후 가능합니다.

About

현재 월 700명 신규수강중입니다.

  (09:00~20:00) 빠른 상담은 카톡 플러스친구 코딩애플 (링크)
  admin@codingapple.com
  이용약관
ⓒ Codingapple, 강의 예제, 영상 복제 금지
top

© Codingapple, All rights reserved. 슈퍼로켓 에듀케이션 / 서울특별시 강동구 고덕로 19길 30 / 사업자등록번호 : 212-26-14752 온라인 교육학원업 / 통신판매업신고번호 : 제 2017-서울강동-0002 호 / 개인정보관리자 : 박종흠