ABOUT ME

탈개발하고 기획자가 된 블로거입니다. 관심사에 대해 올려요.

Today
Yesterday
Total
  • [React] SetState
    ComputerScience/React 2020. 7. 11. 21:22
    class App extends React.Component {
    state = {
    	count : 0,
    };
    
    
    add = () => {
    	(생략...)
    };
    
    (생략...)
    }

     

     

      add = () => {
        //this.setState({ count: this.state.count + 1 });
        this.setState(current => ({
          count : current.count +1,
        }));
      };

     

    add함수로 setState함수를 호출한다.

    current에는 현재 state가 넘어온다.

     

    state에 setState에 접근하여 변경시킬 수 있다.

    state는 직접 접근해선 안됨.

     

    'ComputerScience > React' 카테고리의 다른 글

    댓글

Designed by Tistory.