0
繼承人我的路線:路由器呈現意想不到的訂單成分,並導致proptype警告
<Router history={browserHistory}>
<Route path="/" component={App}>
<IndexRedirect to="/Container/1" />
<Route path="Container" component={Container}>
<Route path="1" component={Component1} />
<Route path="2" component={Component2} />
<Route path="3" component={Component3} />
<IndexRedirect to="1" />
</Route>
</Route>
</Router>
所以路由器是負責哪一個組件內Container
渲染。 Container
將連接到終極版,並將其應用於使用下面的函數獲取它的孩子們的道具:
renderChildren() {
return React.Children.map(this.props.children,
(child) => React.cloneElement(child, {
...this.props
})
);
}
在Component1
我已經定義proptypes:
Component1.propTypes = {
prop1: PropTypes.number,
prop2: PropTypes.func.isRequired
}
如果我把一個debugger
聲明渲染功能的Component1
和Container
,Component1
實際上會先呈現,並且不會有任何道具傳遞給它。這發出警告說prop2是必需的。在此之後,Container
被渲染,然後Component1
再次與道具和警告消失。
如何避免這些警告?我打破了最佳做法嗎?
我想我並沒有包括它的問題,但我導入PropTypes:'進口反應,{} PropTypes從「反應」;'我100%肯定這不是問題的原因。 – Dave