reactjs - 如何修复 react-redux 中的“TypeError:this.props.messages.map 不是函数”
问题描述
我正在尝试通过 mapDispatchToProps() 将道具传递给我的组件。但是,我遇到了这个问题
TypeError: this.props.messages.map is not a function
DisplayMessages.render
C:/Users/User/Desktop/Project/first-react-redux/src/DisplayMessages.js:38
35 | <h2>Type in a new message</h2>
36 | <input value={input} onChange={this.handleChange} />
37 | <button onClick={this.submitMessage}>Submit</button>
> 38 | <ul>{this.props.messages.map((message, index) => <li key={index}>{message}</li>)}</ul>
| ^ 39 | </div >
40 | )
41 | }
如果我取消注释第 38 行,我的页面可以显示。但是,单击按钮后,我收到此错误消息:
TypeError: Object(...) is not a function
submitNewMessage
C:/Users/User/Desktop/Project/first-react-redux/src/DisplayMessagesContainer.js:18
16 | return {
17 | submitNewMessage: (newMessage) => {
> 18 | dispatch(addMessage(newMessage))
19 | }
20 | }
21 | };
我是 React-redux 的新手,我不确定出了什么问题。我目前正在处理的示例来自 freecodecamp,我已经在代码编辑器中测试了这些代码并通过了挑战(除了这次我是在自己的机器上进行并构建自己的文件树。)
这就是我的组件的样子:
import React from 'react';
export default class DisplayMessages extends React.Component {
constructor(props) {
super(props);
this.state = {
input: '',
}
}
handleChange = (event) => {
this.setState({
input: event.target.value
})
}
submitMessage = () => {
this.props.submitNewMessage(this.state.input)
this.setState({
input: '',
})
}
render() {
const { input} = this.state
return (
<div>
<h2>Type in a new message</h2>
<input value={input} onChange={this.handleChange} />
<button onClick={this.submitMessage}>Submit</button>
<ul>{this.props.messages.map((message, index) => <li key={index}>{message}</li>)}</ul>
</div >
)
}
}
这是我的容器:
import DisplayMessages from './DisplayMessages';
import { connect } from 'react-redux';
import addMessage from './redux/actions'
mapStateToProps
const mapStateToProps = (state) => {
return { messages: state }
};
const mapDispatchToProps = (dispatch) => {
return {
submitNewMessage: (message) => {
dispatch(addMessage(message))
}
}
};
const DisplayMessagesContainer = connect(mapStateToProps, mapDispatchToProps)(DisplayMessages)
export default DisplayMessagesContainer
我的 reducer.js:
import { combineReducers } from 'redux';
import types from './types';
const defaultState = [];
const messageReducer = (state = defaultState, action) => {
switch (action.type) {
case types.ADD:
return [...state].concat(action.message)
break;
default:
return state
}
}
const rootReducer = combineReducers({
message: messageReducer
});
export default rootReducer;
我为动作创建者准备的 action.js:
import types from './types.js';
const addMessage = (message) => {
return {
type: types.ADD,
message: message
}
}
export default {
addMessage,
}
还有我的 type.js:
const ADD = "ADD"
export default {
ADD,
}
那么我的代码到底出了什么问题呢?
对此的任何帮助都非常感谢。
谢谢
编辑:我已经确认 this.props.messages 是一个对象而不是一个数组。但我的进一步问题是:为什么我能够在 freecodecamp 的代码编辑器中映射对象,但现在不行?我该如何解决这个问题?
解决方案
@chubbybunnymap function
仅适用于数组。检查是否this.props.messages
是一个数组。
推荐阅读
- algorithm - 如何使用信标技术通过 trilat 库计算精确位置
- android - 当此 USB 设备连接或自动关闭弹出窗口时,如何防止 android 将弹出窗口作为打开的应用程序
- c++ - Visual Studio (C++) 2019 在调试模式下不内联函数
- css - 如何将所有 scss 文件编译到所需路径
- java - 为什么 Quarkus 不扫描 JakartaEE bean?
- java - 我正在自动化一个百思买应用程序。我收到了一种邮件 ID 弹出窗口如何解决它
- java - 将目标 SDK 从 28 更改为 29 导致 BLE 扫描不起作用
- c++ - 防止 C++ 中矢量化的数学函数
- javascript - 自动化 windows Chrome 异常 selenium
- azure - 当存在带有转换的数据流活动时,Azure 数据工厂转换管道的执行时间超过 5 分钟