简体   繁体   English

无法读取 React-redux 上未定义错误的属性“地图”,尽管使用道具而不是 state

[英]cannot read property 'map' of undefined error on React-redux, despite using props instead of state

I missed sth but idk really.我错过了,但我真的很想念。 In similar questions, I saw reason of this error is using this.sth.state.map() instead of this.sth.props.map() .在类似的问题中,我看到此错误的原因是使用this.sth.state.map()而不是this.sth.props.map() But I use props.但我使用道具。 I pull my datas from api which I create.我从我创建的 api 中提取数据。 I gave products:[] as initial state.我给products:[]作为初始 state。

Btw, I did it for categories too.顺便说一句,我也为类别做过。 I create categories: [] and it works well.我创建categories: []并且效果很好。 I can see my categories on my browser.我可以在浏览器上看到我的类别。 I have done same things again for products but im in trouble with map func.我再次为产品做了同样的事情,但我遇到了 map 功能的麻烦。 this time.这次。 What could be the problem?可能是什么问题呢?

When I delete the code that I mentioned below with arrow, I didn't get any error but I couldn't see products.当我用箭头删除下面提到的代码时,我没有收到任何错误,但我看不到产品。

My ProductList.js file;我的 ProductList.js 文件;

import React, { Component } from "react";
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import { Badge } from "reactstrap";
import { Table } from "reactstrap";
import * as productActions from "../../redux/actions/productActions";

class ProductList extends Component {
  componentDidMount() {
    this.props.actions.getProducts();
  }
  render() {
    return (
      <div>
        <Badge color="warning">Products of </Badge>
        <Badge color="success">{this.props.currentCategory.categoryName}</Badge>
        <Table>
          <thead>
            <tr>
              <th>#</th>
              <th>Product Name</th>
              <th>Unit Price</th>
              <th>Quantity Per Unit</th>
              <th>Unit In Stock</th>
            </tr>
          </thead>
          <tbody>
  ------->> {this.props.products.map(product => (
              <tr key={product.id}>
                <th scope="row">{product.id}</th>
                <td>Mark</td>
                <td>Otto</td>
                <td>@mdo</td>
                <td></td>
              </tr>
            ))}
          </tbody>
        </Table>
      </div>
    );
  }
}

function mapStateToProps(state) {
  return {
    currentCategory: state.changeCategoryReducer,
    products: state.ProductListReducer
  };
}

function mapDispatchToProps(dispatch) {
  return {
    actions: {
      getProducts: bindActionCreators(productActions.getProducts, dispatch)
    }
  };
}

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

I set my initial state below as initialState.js我将下面的初始 state 设置为 initialState.js

export default {
    currenCategory:{},
    categories :[ ],
    products : []
}

And productActions.js;和productActions.js;

import * as actionTypes from "./actionTypes";


export function getProductsSuccess(products){
    return {type:actionTypes.GET_PRODUCTS_SUCCESS, payload:products}
}


export function getProducts() {
  return function(dispatch) {
    let url = "http://localhost:3000/products";
    return fetch(url)
      .then(response => response.json())
      .then(result => dispatch(getProductsSuccess(result)));
  };
}


ProductListReducer.js; ProductListReducer.js;

import * as actionTypes from '../actions/actionTypes'
import initialState from './initialState';

export default function productListReducer(state=initialState.products, action){
    switch (action.type) {
        case actionTypes.GET_PRODUCTS_SUCCESS:

            return action.payload

        default:
            return state;
    }
}

I combine reducers in my index.js我在我的 index.js 中结合了减速器

import {combineReducers} from "redux"
import changeCategoryReducer from './changeCategoryReducer'
import categoryListReducer from "./categoryListReducer"
import productListReducer from "./productListReducer"

const rootReducer = combineReducers({
    changeCategoryReducer,
    categoryListReducer,
    productListReducer
})

export default rootReducer;

My store.js;我的商店.js;

export default function configureStore(){
    return createStore(rootReducer,applyMiddleware(thunk))
}

And I add my store to my main index.js within the provider brackets.我将我的商店添加到我的主要 index.js 中的提供方括号中。

But I think the problem is about sth minor for ex.但我认为这个问题与前任有关。 about a defination.关于一个定义。 Because, I did same things while listing categories and it works fine.因为,我在列出类别时做了同样的事情,而且效果很好。

In your component you have used the wrong variable from the store.在您的组件中,您使用了商店中的错误变量。

function mapStateToProps(state) {
  return {
    currentCategory: state.changeCategoryReducer,
    products: state.productListReducer
  };
}

If you notice the P in store is small where as in component you have adde in capital letters.如果您注意到商店中的P很小,那么您在组件中添加了大写字母。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React-redux:无法使用connect获取作为道具的商店,无法读取未定义错误的属性“ props” - React-redux: cannot fetch the store as props using connect, Cannot read property 'props' of undefined error 错误:类型错误:无法读取 React-Redux 中未定义的属性“map” - Error: TypeError: Cannot read property 'map' of undefined in React-Redux 未捕获的TypeError:无法读取react-redux中未定义的属性&#39;props&#39; - Uncaught TypeError: Cannot read property 'props' of undefined in react-redux 在执行 react-redux 项目时出现此错误。 错误:- TypeError:无法读取 React-Redux 项目中未定义的属性“地图” - Getting this error while doing react-redux project. Error:- TypeError: Cannot read property 'map' of undefined in React-Redux project TypeError:无法读取react-redux中未定义的属性“map” - TypeError: Cannot read property 'map' of undefined in react-redux 类型错误:无法使用 react-redux 读取未定义的属性“地图” - TypeError: Cannot read property 'map' of undefined with react-redux 类型错误:无法读取未定义 React-Redux 的属性“地图” - TypeError: Cannot read property 'map' of undefined React-Redux React-Redux:无法读取未定义的属性“地图” - React-Redux: Cannot read property 'map' of undefined 为什么会出现“ TypeError:无法读取未定义的属性” props”(反应路由器,React-Redux)? - Why am I getting 'TypeError: Cannot read property 'props' of undefined' (react-router, React-Redux)? React Redux:无法读取未定义的属性“道具” - React Redux: Cannot read property 'props' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM