简体   繁体   English

在执行 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

I wan to render a bunch of json data in the HTML by using GET request.我想通过使用 GET 请求在 HTML 中呈现一堆 json 数据。 I'm using react and redux to manage the state of the objects, I getting this error.我正在使用 react 和 redux 来管理对象的 state,我收到此错误。 TypeError: Cannot read property 'map' of undefined TypeError:无法读取未定义的属性“地图”

Here's the code这是代码

Action行动

    import { GET_ITEM, ADD_ITEM, DELETE_ITEM } from "./types";

export const getItems = () => {
  return {
    type: GET_ITEM,
  };
};

Reducer减速器


    import { v4 as uuid } from "uuid";
import { GET_ITEM, ADD_ITEM, DELETE_ITEM } from "../actions/types";

const initialState = [
  { id: uuid(), name: "Eggs" },
  { id: uuid(), name: "Milk" },
  { id: uuid(), name: "Bread" },
  { id: uuid(), name: "Water" },
];

const itemReducer = (state = initialState, action) => {
  switch (action.type) {
    case GET_ITEM:
      console.log("inside reducer");
      return {
        ...state,
      };
    default:
      return state;
  }
};
export default itemReducer;

Component零件

    import React, { Component } from "react";
import "../App.css";
import { v4 as uuid } from "uuid";
import { Container, Button, ListGroup, ListGroupItem } from "reactstrap";
import { TransitionGroup, CSSTransition } from "react-transition-group";
import { getItems } from "../actions/itemActions";
import { connect } from "react-redux";
import propTypes from "prop-types";

class ShoppingList extends Component {
  componentDidMount() {
    console.log("inside componentDidMount");
    this.props.getItems();
  }

  onClick = () => {
    const name = prompt("Enter name");
    if (name) {
      this.setState((state) => ({
        items: [...state.items, { id: uuid(), name: name }],
      }));
    }
  };

  render() {
    const items = this.props.items;
    return (
      <div>
        <Container>
          <Button
            color="dark"
            style={{ marginBottom: "2em" }}
            onClick={this.onClick}
          >
            Add Item
          </Button>
          <ListGroup>
            <TransitionGroup>
              {items.map(({ id, name }) => (
                <CSSTransition key={id} timeout={500} classNames="fade">
                  <ListGroupItem>
                    <Button
                      className="remove-btn"
                      color="danger"
                      size="sm"
                      onClick={() => {
                        this.setState({
                          items: items.filter((item) => item.id !== id),
                        });
                      }}
                    >
                      &times;
                    </Button>
                    {name}
                  </ListGroupItem>
                </CSSTransition>
              ))}
            </TransitionGroup>
          </ListGroup>
        </Container>
      </div>
    );
  }
}

const mapStateToProps = (state) => ({ items: state.item.items });

export default connect(mapStateToProps, { getItems })(ShoppingList);

Index reducer, where i combine everything索引缩减器,我将所有内容组合在一起


    import { combineReducers } from "redux";
import itemReducer from "./itemReducer";

export default combineReducers({
  item: itemReducer,
});

Configure Store, where i store the initial state and the reducer配置存储,我存储初始 state 和减速器


import { applyMiddleware, createStore, compose } from "redux";
import thunk from "redux-thunk";
import rootReducer from "./reducers";

const initialtState = {};

const middelware = [thunk];

const store = createStore(
  rootReducer,`enter code here`
  initialtState,
  compose(
    applyMiddleware(...middelware),
    window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
  )
);

export default store;

You didn't define items on your reducer.您没有在减速器上定义项目。 Try this:尝试这个:

const initialState = {
  items: [
    { id: uuid(), name: "Eggs" },
    { id: uuid(), name: "Milk" },
    { id: uuid(), name: "Bread" },
    { id: uuid(), name: "Water" },
  ]
};

暂无
暂无

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

相关问题 错误:类型错误:无法读取 React-Redux 中未定义的属性“map” - Error: TypeError: Cannot read property 'map' of undefined in React-Redux React-redux 在 TypeError 中渲染帖子时出错:无法读取未定义的属性“地图”但控制台具有所有帖子 - React-redux Getting error while rendering post in TypeError:Cannot read property 'map' of undefined but console having all posts 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 - TypeError: Cannot read properties of undefined (reading 'map') “未处理的拒绝(TypeError):无法读取未定义的属性'dispatch'” react-redux 错误 - “Unhandled Rejection (TypeError): Cannot read property 'dispatch' of undefined” react-redux error React-Redux TypeError:无法读取未定义的属性“ setStatus” - React-Redux TypeError: Cannot read property 'setStatus' of undefined 未捕获的TypeError:无法读取react-redux中未定义的属性&#39;props&#39; - Uncaught TypeError: Cannot read property 'props' of undefined in react-redux TypeError:无法读取未定义的属性“操作”(React-Redux) - TypeError: Cannot read property 'actions' of undefined (React-Redux)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM