🚧

[React] render methods should be a pure function of props and state

purpplee 2021. 12. 3. 10:51

render methods should be a pure function of props and state

 

render 내에 setState 등을 ν˜ΈμΆœν•΄μ„œ 계속 λ Œλ”λ§ 되면 이런 였λ₯˜κ°€ λœ¬λ‹€. λ‚œ ν˜ΈμΆœν•œ 적 μ—†λŠ”λ°...?! μ•Œκ³ λ³΄λ‹ˆ μžμ‹ μ»΄ν¬λ„ŒνŠΈλ‘œ setState ν•˜λŠ” function 을 props 둜 전달해쀀 λ’€, onClick 에 ν• λ‹Ήν•΄μ€¬λŠ”λ°.... 이런 ν˜•νƒœλ‘œ ν• λ‹Ήν•΄μ€˜μ„œ 생긴 λ¬Έμ œμ˜€λ‹€.

handleClick = (e) => {
	this.setState({value:e});
}

onClick={handleClick(value)}

 

handleClick(value) λŠ” ν•¨μˆ˜ 싀행문이닀!!!! λ‚œ ν•¨μˆ˜λ₯Ό μ‹€ν–‰ν•˜κ³  μžˆμ—ˆλ˜ 것이닀.... λ”°λΌμ„œ μ•„λž˜μ²˜λŸΌ μˆ˜μ •ν•΄μ€˜μ•Ό ν•œλ‹€.

handleClick = (e) => {
	this.setState({value:e});
}

onClick={() => handleClick(value)}

 

λ°˜μ‘ν˜•