在運行下面的代碼時,我最終得到了CategoryList.js中的錯誤消息「不是對象的值的請求的鍵」。React-Native/Redux錯誤:請求的鍵值不是對象
我相信CategoryContainer.js中的「stores.dispatch(CategoryAction.categoryView())」應該將該值設置爲props類別,但CategoryList.js中的this.props.categories將返回空值,從而導致錯誤。
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中的值。
我嘗試了建議的解決方案,但仍然得到同樣的錯誤。不知道我錯在哪裏。 – ugendrang