React - className 가져오기

da.circle·2022년 11월 30일
0

참고) 태빵- React - 이벤트가 일어나는 요소의 클래스명 가져오기

팔로우 버튼의 클래스 이름에 따라서 실행되는 로직을 다르게 주고 싶다.
요소의 className을 가져오려면 e.target.className으로 가져오면 된다.

const sendResult = e => {
    //클릭했을 때 버튼이 className = 'followBtn'이라면 팔로우 요청
    if (e.target.className === 'followBtn') {
      fetch('http://localhost:8000/follow', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          token: localStorage.getItem('token'),
        },
        body: JSON.stringify({
          following_id: writerInfo.id,
        }),
      });
      //클릭했을 때 버튼이 className = 'followingBtn'이라면 언팔로우 요청
    } else if (e.target.className === 'followingBtn') {
      fetch('http://localhost:8000/follow', {
        method: 'DELETE',
        headers: {
          'Content-Type': 'application/json',
          token: localStorage.getItem('token'),
        },
        body: JSON.stringify({
          following_id: writerInfo.id,
        }),
      });
    }
  };
profile
프론트엔드 개발자를 꿈꾸는 사람( •̀ ω •́ )✧

0개의 댓글