[英]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.