ComputerScience/React
-
[React] componentDidMount(), setTimeout()ComputerScience/React 2020. 7. 11. 21:56
componentDidMount(){ setTimeout(() => { this.setState({isLoading : false}); }, 6000); } App 컴포넌트가 그려지면 (render() 함수가 실행되면) 호출되는 생명주기 함수는 componentDidMount() 이다. setTimeout() 첫번째 인자로 전달한 함수를 두번째 인자로 전달한 값 후에 실행한다. 코드에서 setState함수를 6000ms 즉, 6초 뒤에 출력해준다.
-
[React] SetStateComputerScience/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는 직접 접근해선 안됨.