React-Native/Redux Error: Requested keys of a value that is not an object

React-Native/Redux Error: Requested keys of a value that is not an object

在 运行 下面的代码中,我在 CategoryList.js 中得到错误消息 "Requested keys of a value that is not an object"。

我相信 CategoryContainer.js 中的 "stores.dispatch(CategoryAction.categoryView())" 应该将值设置为道具类别,但是 CategoryList.js 中的 this.props.categories returns 空值导致错误。

ConfigureStore.js:

import {createStore, applyMiddleware} from 'redux';
import reducers from '../reducers';
import thunk from 'redux-thunk';

const createStoreWithMiddleware = applyMiddleware(thunk)(createStore);
const store = createStoreWithMiddleware(reducers);
export default store;

CategoryContainer.js:

import React, { Component } from 'react';
import stores from '../stores/ConfigureStore';
import * as CategoryAction from '../actions/CategoryAction';

stores.dispatch(CategoryAction.categoryView());

class CategoryContainer extends Component {
  render() {
    return (
      <CategoryList/>
    );
  }
}

const mapStateToProps = (state) => {
  return {
    categories: state.categories,
  };
}

const mapDispatchToProps = (dispatch) => {
  return {
    bindActionCreators(CategoryAction, dispatch)
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(CategoryContainer);

CategoryAction.js:

import * as actionTypes from './ActionTypes';
import AppConstants from '../constants/AppConstants';

export function categoryView() {
  const categories = [{name:'CATEGORY', type:'CATGORY_TYPE'}];
  return {
      type: "CATEGORY_VIEW",
      categories: categories
  };
}

CategoryReducer.js:

const initialState = {
  categories:[]
}

export default function categoryReducer (state = initialState, action) {
  switch (action.type) {
    case "CATEGORY_VIEW":
      return Object.assign({}, state, {
              categories: action.categories
            });
    }
}

CategoryList.js:

import React, {Component} from 'react';
import {
  Text, View, TouchableHighlight, TouchableOpacity, ListView, StyleSheet
} from 'react-native';

import * as AppConstants from '../constants/AppConstants';
import CategoryAdd from '../components/CategoryAdd';

export default class CategoryList extends Component {

  constructor(props) {
    super(props);

    this.ds = new ListView.DataSource({rowHasChanged: (row1, row2) => row1 !== row2})
    this.state = {
      dataSource: this.ds.cloneWithRows(this.props.categories),
    }
  }
}

我什至在 CategoryContainer.js 中尝试过如下方法,但没有帮助。仍然出现同样的错误,

<CategoryList {...this.props}/>

但是,如果我通过将 this.props.categories 替换为下面的常量值来更改 CategoryList.js,它会起作用。

    const categories = [{name:'CATEGORY', type:'CATGORY_TYPE'}];
    this.state = {
      dataSource: this.ds.cloneWithRows(categories),
    }

请协助在 redux flow 中设置 this.props.categories 中的值。

你应该像这样更新 reducer 中的 categories 数组 -

  return Object.assign({}, state, {
    categories: Object.assign([], state.categories, action.categories)
  });

然后在容器中做<CategoryList {...this.props}/>

CategoryContainer.js 中的以下更改使其有效,

    categories: state.categoryReducer.categories,
    modal: state.categoryReducer.modal