reactjs - TypeError: Cannot read property 'value' of undefined 使用 Redux 向数组添加值时
问题描述
计数器容器
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import PropType from 'prop-types';
import Counter from '../components/Counter';
import * as counterActions from '../store/modules/counter';
import * as postActions from '../store/modules/post';
class CounterContainer extends Component {
handleIncrement = () => {
const { CounterActions } = this.props;
CounterActions.increment();
}
handleDecrement = () => {
const { CounterActions } = this.props;
CounterActions.decrement();
}
addDummy = () => {
const { PostActions } = this.props;
console.log(PostActions);
PostActions.addDummy({
content: 'dummy',
userUID: 123,
});
}
render() {
const { handleIncrement, handleDecrement, addDummy } = this;
const { number } = this.props;
return (
<Counter
onIncrement={handleIncrement}
onDecrement={handleDecrement}
addDummy={addDummy}
number={number}
/>
);
}
}
CounterContainer.propTypes = {
number: PropType.number.isRequired,
CounterActions: PropType.shape({
increment: PropType.func,
decrement: PropType.func,
}).isRequired,
};
export default connect(
state => ({
number: state.counter.number,
}),
dispatch => ({
CounterActions: bindActionCreators(counterActions, dispatch),
PostActions: bindActionCreators(postActions, dispatch),
}),
)(CounterContainer);
邮政容器
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
// import { ListItem } from 'react-native-elements';
import { Text } from 'react-native';
import PropType from 'prop-types';
import Post from '../components/Post';
import * as postActions from '../store/modules/post';
class PostContainer extends Component {
refreshing = () => {}
onRefresh = () => {}
renderItem = ({ item }) => (<Text>{item.content}</Text>)
render() {
const { renderItem } = this;
const { postList } = this.props;
return (
<Post
postList={postList}
renderItem={renderItem}
/>
);
}
}
export default connect(
state => ({
postList: state.post.postList,
}),
dispatch => ({
CounterActions: bindActionCreators(postActions, dispatch),
}),
)(PostContainer);
模块/帖子
import { createAction, handleActions } from 'redux-actions';
const initialState = {
postList: [{
content: 'test',
userUID: 123,
},
{
content: '123123',
userUID: 123123,
},
],
};
const ADD_DUMMY = 'ADD_DUMMY';
export const addDummy = createAction(ADD_DUMMY, ({ content, userUID }) => ({ content, userUID }));
const reducer = handleActions({
[ADD_DUMMY]: (state, action) => ({
...state,
postList: [action.data, ...state.postList],
}),
}, initialState);
export default reducer;
单击该按钮会在 postList 中添加一个虚拟对象。但是,当我单击按钮时,我得到
TypeError:无法读取未定义错误的属性“内容”。
我以为我做的和倒计时教程一样。但是倒计时有效。添加我制作的假人不起作用。哪里出错了?
直到我单击 Add Dummy Data 按钮列表才有效。
解决方案
我改变action.data -> actions.payload
const reducer = handleActions({
[ADD_DUMMY]: (state, action) => ({
...state,
postList: [action.payload, ...state.postList],
}),
}, initialState);
这只是一个错误。
推荐阅读
- php - 仅当不活动时间介于 24 分钟和 55 分钟之间时,PHP 才会丢弃 session-cookie
- javascript - 以多步形式将单选值传递到 NEXT 选项卡
- auth0 - Auth0 的时区配置
- jmeter - 如何在不同实例上为我的 JMeter 脚本使用不同的 CSV
- javascript - 如何让它打印为空
- django - 关于如何在 Django For Heroku 上设置 Quotaguard 的详细说明
- selenium - 会话未创建:此版本的 ChromeDriver 仅支持 Chrome 版本 89 当前浏览器版本为 91.0.4472.77
- android-viewpager2 - 带有 TabLayout 的 ViewPager2 滑动到 Tab 上的下一个位置
- javascript - 向同位素网格添加分页
- android - 无法安装应用程序,Android Studio