https://ko.reactjs.org/docs/render-props.html
https://www.robinwieruch.de/react-render-props-pattern/
class Amount extends Component {
...
render() {
return (
<div>
<span>US Dollar: {this.state.amount} </span>
<button type="button" onClick={this.onIncrement}>
+
</button>
<button type="button" onClick={this.onDecrement}>
-
</button>
<Euro amount={this.state.amount} />
<Pound amount={this.state.amount} />
</div>
);
}
}
<Euro>
, <Pound>
등의 컴포넌트를 <Amount> 내부가 아닌 외부에서 설정 하고 싶은 경우가 있다.const App = () => (
<div>
<Amount />
<Euro amount={amount} />
<Pound amount={amount} />
</div>
);
<Euro>
, <Pound>
에서 알지 못하기 때문에 상태들을 App
으로 끌어 올려서 관리해야 한다.const App = () => (
<Amount>
<Pound amount={amount} />
<Euro amount={amount} />
</Amount>
);
{this.props.children && this.props.children.map((option) => {
const childrenWithProps = React.Children.map(option, child => React.cloneElement(child, { handleOption: this.handleOption }));
return (
childrenWithProps
);
})}