arcthur / react-book-examples

深入 React 技术栈一书中示例

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Tabs例子问题

zenjava opened this issue · comments

Tabs例子,采用下拉框无法正确切换Tab页。

原因是你外部控制的选中页状态,是在构造函数中进行处理。而构造函数与didMount事件是只有第一次render时候才会触发的。


Tabs的componentWillReceiveProps实现改为下面的实现即可。
componentWillReceiveProps(nextProps) {
if ('defaultActiveIndex' in nextProps) {
this.setState({
activeIndex: nextProps.defaultActiveIndex,
});
}
}

我仔细看了例子,点了没有反应是对的。书里也提到了内部控制和外部控制,
defaultActiveIndex 是内部控制,在 app 中写的就是 defaultActiveIndex,外部要控制 activeIndex 是没有作用的。把 prop 改成 activeIndex,就变成外部受控了,你可试下