React-Native / Redux错误:请求的值不是对象的键

时间:2017-02-28 15:11:31

标签: react-native react-redux

在运行以下代码时,我最终得到了CategoryList.js中的错误消息“请求的值不是对象的键”。

我相信CategoryContainer.js中的“stores.dispatch(CategoryAction.categoryView())”应该将值设置为props类别,但CategoryList.js中的this.props.categories会返回null值,从而导致错误。

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中的值。

2 个答案:

答案 0 :(得分:1)

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

elevation

然后在容器中执行categories

答案 1 :(得分:0)

在CategoryContainer.js下面的变化使其有效,

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