【发布时间】:2016-08-10 14:32:44
【问题描述】:
我将 observable-react-project 转换为 Redux。我有主容器 - App.js 和他包含下一个方法:
handleDone () {
this.setState({ showPLock: false });
}
render () {
return (
<div className="mainContainer">
{this.props.children}
{
this.state.showPLock
? <PCode handleDone={this.handleDone.bind(this)}/>
: null
}
</div>
);
}
在第一次启动应用中显示PLock: true。 代码 PCode:
class PCode extends Component{
constructor (props) {
super(props);
this.state = {
passcode: ''
};
}
render () {
//something doing
}
}
PCode.contextTypes = {
router: PropTypes.object.isRequired,
store: PropTypes.object.isRequired
};
PCode.propTypes = {
dispatch: PropTypes.func.isRequired,
};
function mapStateToProps(stateStore) {
const { authReducer } = stateStore;
if (authReducer.pcode !== null) {
//something return
}
}
export default connect(mapStateToProps)(PCode);
调用 mapStateToProps 时出现问题。在组件命中时不会立即调用他。因此,如果我调用 render 一些东西,例如:
const { data } = this.props;
当然会发出undefined。
为什么第一次调用组件时没有调用 mapStateToProps?
【问题讨论】:
标签: reactjs redux react-redux