React TypeError: Cannot read property 'categoryName' of undefined problem
React TypeError: Cannot read property 'categoryName' of undefined problem
我正在尝试从 initialState 获取我的类别名称,但它显示“类型错误:无法读取 属性 'categoryName' 未定义问题”。我该如何解决这个案子?
这是我的 initialState.js 文件:
export default {
currentCategory:{categoryName:"Beverages"},
categories:[]
};
这是我的 categoryList.js 文件:
import React, { Component } from "react";
import { connect } from "react-redux";
import {bindActionCreators} from 'redux'
import * as categoryActions from '../../redux/actions/categoryActions'
class CategoryList extends Component {
componentDidMount(){
this.props.actions.getCategories()
}
render() {
return (
<div>
<h3>Categories {this.props.categories.legnth}</h3>
<h5>Seçili Kategori: {this.props.currentCategory.categoryName}</h5>
</div>
);
}
}
function mapStateToProps(state) {
return {
currentCategory: state.changeCategoryReducers,
categories:state.categoryListReducer
}
}
function mapDispatchToProps(dispatch){
return {
actions:{
getCategories:bindActionCreators(categoryActions.getCategories,dispatch)
}
}
}
export default connect(mapStateToProps,mapDispatchToProps)(CategoryList);
我正在使用 reducers、redux 和 thunk,但我遇到了一个问题,我认为应该是安装问题。但是我无法解决遗漏了哪个包裹。对于任何建议都会非常有效。
这是我的 changeCategoryReducer.js:
import * as actionTypes from "../actions/actionTypes";
import initialState from "./initialState";
export default function changeCategoryReducer(
state = initialState.currentCategory,
action
) {
switch (action.type) {
case actionTypes.CHANGE_CATEGORY:
return action.payload;
default:
return state;
}
}
这是我的 index.js 文件:
import { combineReducers } from "redux";
import changeCategoryReducer from "./changeCategoryReducer";
import categoryListReducer from './categoryListReducer';
const rootReducer = combineReducers({
changeCategoryReducer,
categoryListReducer,
});
export default rootReducer;
我没有看到这一行:currentCategory: state.changeCategoryReducers,
但是@Tauseef Ahmad 看到了。
应该是这样的:currentCategory: state.changeCategoryReducer,
我正在尝试从 initialState 获取我的类别名称,但它显示“类型错误:无法读取 属性 'categoryName' 未定义问题”。我该如何解决这个案子? 这是我的 initialState.js 文件:
export default {
currentCategory:{categoryName:"Beverages"},
categories:[]
};
这是我的 categoryList.js 文件:
import React, { Component } from "react";
import { connect } from "react-redux";
import {bindActionCreators} from 'redux'
import * as categoryActions from '../../redux/actions/categoryActions'
class CategoryList extends Component {
componentDidMount(){
this.props.actions.getCategories()
}
render() {
return (
<div>
<h3>Categories {this.props.categories.legnth}</h3>
<h5>Seçili Kategori: {this.props.currentCategory.categoryName}</h5>
</div>
);
}
}
function mapStateToProps(state) {
return {
currentCategory: state.changeCategoryReducers,
categories:state.categoryListReducer
}
}
function mapDispatchToProps(dispatch){
return {
actions:{
getCategories:bindActionCreators(categoryActions.getCategories,dispatch)
}
}
}
export default connect(mapStateToProps,mapDispatchToProps)(CategoryList);
我正在使用 reducers、redux 和 thunk,但我遇到了一个问题,我认为应该是安装问题。但是我无法解决遗漏了哪个包裹。对于任何建议都会非常有效。 这是我的 changeCategoryReducer.js:
import * as actionTypes from "../actions/actionTypes";
import initialState from "./initialState";
export default function changeCategoryReducer(
state = initialState.currentCategory,
action
) {
switch (action.type) {
case actionTypes.CHANGE_CATEGORY:
return action.payload;
default:
return state;
}
}
这是我的 index.js 文件:
import { combineReducers } from "redux";
import changeCategoryReducer from "./changeCategoryReducer";
import categoryListReducer from './categoryListReducer';
const rootReducer = combineReducers({
changeCategoryReducer,
categoryListReducer,
});
export default rootReducer;
我没有看到这一行:currentCategory: state.changeCategoryReducers,
但是@Tauseef Ahmad 看到了。
应该是这样的:currentCategory: state.changeCategoryReducer,