我仍然是React的小老鼠,在互聯網上的很多例子中,我看到了這種變化,在渲染子元素時我感到困惑。通常我看到這一點:什麼時候應該使用React.cloneElement vs this.props.children?
class Users extends React.Component {
render() {
return (
<div>
<h2>Users</h2>
{this.props.children}
</div>
)
}
}
但後來我看到這樣一個例子:
<ReactCSSTransitionGroup
component="div"
transitionName="example"
transitionEnterTimeout={500}
transitionLeaveTimeout={500}
>
{React.cloneElement(this.props.children, {
key: this.props.location.pathname
})}
</ReactCSSTransitionGroup>
現在我明白了API,但docs不完全清楚的時候,我應該使用它。
那麼其他人不能做什麼?有人可以用更好的例子向我解釋這一點嗎?
https://www.youtube.com/watch?v=hEGg-3pIHlE這傢伙正在展示他如何使用cloneElement。看看這個例子 – yuyokk