TypeError:使用Redux向数组添加值时,无法读取未定义的属性“值”

时间:2019-05-08 06:51:22

标签: reactjs react-native react-redux

CounterContainer

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);

PostContainer

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:无法读取未定义错误的属性“ content”。

我认为我做到了与递减计数教程相同。 但是倒数计时有效。 添加我制作的假人无效。 哪里出问题了?

在我单击“添加虚拟数据”按钮之前 该列表已完成。

1 个答案:

答案 0 :(得分:0)

我更改action.data -> actions.payload

const reducer = handleActions({
  [ADD_DUMMY]: (state, action) => ({
    ...state,
    postList: [action.payload, ...state.postList],
  }),
}, initialState);

这只是一个错误。