【发布时间】:2018-01-31 23:24:14
【问题描述】:
我有以下父组件:
import React, { Component, PropTypes } from 'react'
import { connect } from 'react-redux';
import {bindActionCreators} from 'redux';
import _ from "lodash";
import ChildComponent from "./ChildComponent";
class ParentComponent extends Component {
constructor(props) {
super(props);
this.state = {
};
}
render() {
return (
<div>
I'm at Parent
<ChildComponent/>
</div>
);
}
}
function mapStateToProps(state) {
return { }
}
export default connect(mapStateToProps, null)(ParentComponent);
在父级内部有一个名为 ChildComponent 的组件,如下所示:
import React, { Component, PropTypes } from "react";
import { connect } from "react-redux";
import { reduxForm } from "redux-form";
import { bindActionCreators } from "redux";
class ChildComponent extends Component {
constructor(props) {
super(props);
}
componentWillMount() {
}
render() {
return (
<div>
at the child
</div>
);
}
}
function mapDispatchToProps(dispatch) {
return bindActionCreators(
{
},
dispatch
);
}
function mapStateToProps(state) {
return {
};
}
export default connect(mapStateToProps, mapDispatchToProps)(
ChildComponent
);
但如果我点击继续,页面会恢复正常。我不明白子组件是如何未定义的。它只是嵌入的,不包含任何道具。
更新: 我不再收到错误消息,但我注意到当我打开这个特定组件时我的页面变为空白。我会做更多的故障排除。
【问题讨论】:
-
将
export default connect(mapStateToProps, mapDispatchToProps)( ChildComponent );更改为export default ChildComponent,看看问题是否仍然存在。此外,在简单的 repo、fiddle、codepen 或 codesandbox 中重现问题会更容易,因此我们可以轻松调试它。
标签: reactjs components parent