尝试从react-redux使用connect()函数时,我收到以下错误:
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
Check the render method of `App`.
这是应用程序:
import { Provider } from 'react-redux';
import configureStore from './store';
const App = class extends React.PureComponent {
render() {
const { title } = this.context;
return (
<div className="center-screen">
{title}
<Provider store={configureStore()}>
<Chat />
</Provider>
</div>
);
}
};
这是聊天的相关代码结尾:
import { connect } from 'react-redux';
...
const mapStateToProps = state => ({
...state
});
const mapDispatchToProps = dispatch => ({
addMessage: () => dispatch(addMessage)
});
export default connect(
mapStateToProps,
mapDispatchToProps
)(Chat);
使用:“ export default Chat”代替connect时,它工作正常.
解决方法:
尝试这个:
const ConnectedChat = connect(
mapStateToProps,
mapDispatchToProps
)(Chat);
export default ConnectedChat;
或者,您可能希望将类定义重命名为ConnectedChat并反转名称,以便可以将其导入为“聊天”.
编辑:还要确保将聊天组件导入到App文件中,如果没有,请导入addMessage操作创建者.