开发者问题收集

Redux 无法读取未定义的属性图

2018-10-28
244

我正尝试通过 redux 获取一些数据,并尝试在 Fedp.js 组件中呈现它。但我在映射中遇到错误。你能告诉我整个样板中哪里出错了吗?

productActions.js

export function fetchProducts() {
  return function(dispatch) {
    dispatch({
      type: 'FETCH_PRODUCTS_REQUEST'
    });
    return fetch('/products')
      .then(response => response.json().then(body => ({ response, body })))
      .then(({ response, body }) => {
        if (!response.ok) {
          dispatch({
            type: 'FETCH_PRODUCTS_FAILURE',
            error: body.error
          });
        } else {
          dispatch({
            type: 'FETCH_PRODUCTS_SUCCESS',
            products: body.products
          });
        }
      });
    }
 }


productReducer.js

const initialState = {
  products: []
};


export  function productsReducer(state = initialState, action) {
  switch (action.type) {
    case 'FETCH_PRODUCTS_REQUEST':
      return Object.assign({}, state, {
        isFetching: true
      });
    case 'FETCH_PRODUCTS_SUCCESS':
      return Object.assign({}, state, {
        isFetching: false,
        products: action.products
      });
    case 'FETCH_PRODUCTS_FAILURE':
      return Object.assign({}, state, {
        isFetching: false,
        error: action.error
      });
    default:
      return state;
  }
}


indexreducer.js:

import { combineReducers } from 'redux';
import { productsReducer } from './productReducer';

export default combineReducers({
  products: productsReducer
});


configureStore.js

import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from '../reducers/indexReducer';

function configureStore(initialState) {
  return createStore(
    rootReducer,
    initialState,
    applyMiddleware(thunk)
  );
}

export default configureStore


index.js

import React from "react";
import ReactDOM from "react-dom";
import App from './App.js'
import configureStore from './Redux/store/configureStore';
import { Provider } from 'react-redux';



const store = configureStore();


ReactDOM.render( <Provider store={store}><App /></Provider>, document.getElementById('root'))


Fedp.js

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { fetchProducts } from '../Redux/actions/productAction';




class Fedp extends Component {

  componentDidMount() {
    this.props.fetchProducts();
  }


  render() { 
    return (
            <ul>
            {this.props.products.map((details, index)=>{
              <li key={item.id}> {details.name} + {details.price} </li>
        })}
          </ul>
    );
  }
}




function mapStateToProps(state) {
  return {
    products: state.products
  };
}


function mapDispatchToProps(dispatch) {
  return {
    fetchProducts: function() {
      dispatch(fetchProducts());
    }
  };
}




export default connect(mapStateToProps, mapDispatchToProps)(Fedp);
1个回答

mapStateToProps 方法中,您将 state.products 的值作为 products 返回。但 state.products 是 Reducer 本身,而不是您从 API 调用中获取的结果数组。

下面的这个块使用 combineReducers 的参数中提到的所有键创建一个 redux state 对象。

export default combineReducers({
    products: productsReducer
});

所以 state.products 只不过是您的 productsReducer ,如上所示。现在,您从 API 调用中获取的值存储在 productsReducer 内的 products 键中。因此,访问这些值的正确方法是 state.products.products

将您的 mapStateToProps 修改为下面的块,它就可以工作了。

function mapStateToProps(state) {
    return {
        products: state.products.products
    };
}
Anmol Mahatpurkar
2018-10-28