react state


react state

아래와 같이 this.state.count = 1를 하면 react는 render function을 refresh 하지 않는다. 매번 state의 상태를 변경할 때 react가 render function을 호출해서 바꿔줘야한다. 그렇게 바꾸기 위해서는 setState를 이용한다. 일단 state는 object이기에 setState는 새로운 state를 받아야한다. this.setState({count : 1}); 클릭을 할 때 마다 +- 1이 되고 하고 싶다면 this.setState({count : this.state.count + 1}); 하지만 위와 같은 방법은 별로 좋은 방법이 아니다 state를 set 할 때는 react에서 외부의 상태에 의존하지 않는 것이 가장 좋은 방법이다. this.setState(current => ({count : current.count +1}); - 출처 : 노..........



원문링크 : react state