Build React with separation of concerns in mind
2018-04-16
UI๋ฅผ ๋ด๋นํ๋ ์ปดํฌ๋ํธ์ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ๋ ์ปดํฌ๋ํธ๋ฅผ ๋ถ๋ฆฌํ๋ ํจํด์ด๋ค. React ๊ณต์ ๋ฌธ์์ 'Lifting State Up'(๊ณตํต๋ ์กฐ์์ผ๋ก state๋ฅผ ์ฌ๋ ค์ ๊ด๋ฆฌํ๋ ๊ฒ)์ ์์ฉ์ด๋ผ๊ณ ๋ณผ ์ ์๋ค. Separation of concern์ ๋ฐ๋ฅด๊ณ ํนํ stateless component์ ๊ฒฝ์ฐ, ์ฌ์ฌ์ฉ์ด ์ฉ์ดํ๋ค๋ ์ฅ์ ์ด ์๋ค.
์ถ์ฒ:
interface Props {
name: string
}
export const Hello: React.SFC<Props> = ({ name }) => (
<div>Hello, {name}!</div>
)
interface Props {
options: string[]
}
interface State {
selectedOption: string
}
export class SelectOptions extends React.Component<Props, State> {
state = { selectedOption: null }
handleSelectOption = (value: string) => this.setState({ selectedOption: value }) // ๋ฐ์ดํฐ ๋ณ๊ฒฝํ๊ธฐ
render() {
return (
<StatelessComponent
options={this.props.options} // ๋ฐ์ดํฐ ๋ณด๋ด๊ธฐ
onSelectOption={this.handleSelectOption} // ์ฝ๋ฐฑ ๋ณด๋ด๊ธฐ
/>
)
}
}