简体   繁体   English

类型错误:无法读取未定义 React-Redux 的属性“地图”

[英]TypeError: Cannot read property 'map' of undefined React-Redux

I am learning Redux from https://app.pluralsight.com/library/courses/react-redux-react-router-es6/table-of-contents but I am stuck at some point.我正在从https://app.pluralsight.com/library/courses/react-redux-react-router-es6/table-of-contents学习Redux ,但我被困在某个点上。 When I am trying to map my courses in CoursePage.js then it is showing this error, The prop courses is marked as required in CoursesPage , but its value is undefined .当我尝试在 CoursePage.js 中对我的课程进行CoursePage.js时,它会显示错误,道具coursesCoursesPage中标记为必需,但其值是undefined and:和:

Uncaught TypeError: Cannot read property 'map' of undefined.未捕获的类型错误:无法读取未定义的属性“地图”。

Here's the code这是代码

CoursePage.js CoursePage.js

import React, { Component } from "react";
import { connect } from "react-redux";
import * as courseAction from "../../redux/actions/courseAction";

class CoursePage extends Component {
  constructor(props) {
    super(props);
    console.log(this);
    this.state = {
      course: {
        title: "",
      },
    };
  }

  handleChange = (event) => {
    const course = { ...this.state.course, title: event.target.value };
    this.setState({ course });
  };

  handleSubmit = (event) => {
    event.preventDefault();
    this.props.createCourse(this.state.course);
    // alert(this.state.course.title);
  };

  render() {
    return (
      <main>
        <div className="container">
          <div className="jumbotron jumbotron-fluid">
            <div className="container">
              <h1 className="display-4">Course Page</h1>
              <form className="mt-4" onSubmit={this.handleSubmit}>
                <input
                  type="text"
                  onChange={this.handleChange}
                  value={this.state.course.title}
                />

                <input type="submit" value="Add" />

                {this.props.course.map((course) => (
                  <div key={course.title}>{course.title}</div>
                ))}

              </form>
            </div>
          </div>
        </div>
      </main>
    );
  }
}

function mapStateToProps(state) {
  return {
    course: state.course,
  };
}

function mapDispatchToProps(dispatch) {
  return {
    createCourse: (course) => dispatch(courseAction.createCourse(course)),
  };
}

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

courseReducer.js courseReducer.js

import * as types from "../actions/actionTypes";

export default function courseReducer(state = [], action) {
  switch (action.type) {
    case types.CREATE_COURSE:
      return [...state, { ...action.course }];
    default:
      return state;
  }
}

The state called this.state.course is an {} and that's the reason why you cannot run .map() on it. state 称为this.state.course是一个{} ,这就是您无法在其上运行.map()的原因。 It can handle arrays only.它只能处理 arrays。 Instead probably you want to show up title for the course then you don't need .map() .相反,您可能想显示课程的title ,然后您不需要.map()

Try the following only:仅尝试以下操作:

<div>{this.state.course.title}</div>

I hope this helps!我希望这有帮助!

You've got a few minor issues here, that need fixing to bring you on the right track.您在这里遇到了一些小问题,需要解决这些问题才能使您走上正轨。

Your mapStateToProps refers to course , not courses .您的mapStateToProps指的是course ,而不是courses And if you want it to refer to courses then you need to update how the CREATE_COURSE action works in your reducer.如果您希望它引用courses ,那么您需要更新CREATE_COURSE操作在减速器中的工作方式。

To fix the crash causing bug you'll need to add an initialState so that courses isn't undefined.要修复导致错误的崩溃,您需要添加一个initialState以便courses不是未定义的。 Since it's ok to map through an empty array, but you can't map through undefined .由于可以通过空数组 map ,但不能通过undefined map 。

Update your courseReducer to the following:将您的courseReducer更新为以下内容:

import * as types from "../actions/actionTypes";

const initialState = {
  courses: []
}

export default function courseReducer(state = initialState, action) {
  switch (action.type) {
    case types.CREATE_COURSE:
      return { ...state, courses: [...state.courses, action.course] };
    default:
      return state;
  }
}

And then make this change to your mapStateToProps :然后对您的mapStateToProps进行此更改:

function mapStateToProps(state) {
  return {
    courses: state.courses,
  };
}

Finally change this bit of code from this.props.course to this.props.courses :最后将这段代码从this.props.coursethis.props.courses

{this.props.courses.map((course) => (
  <div key={course.title}>{course.title}</div>
))}

Now your app should work as intended.现在您的应用程序应该可以按预期工作了。

暂无
暂无

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

相关问题 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 中未定义的属性“map” - Error: TypeError: Cannot read property 'map' 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 React-Redux - 类型错误:无法读取未定义的属性(读取“地图”) - React-Redux - TypeError: Cannot read properties of undefined (reading 'map') 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) 类型错误:无法读取 react-redux 数组 API 中未定义的属性“映射” - TypeError: Cannot read property 'map' of undefined in react-redux Array API React-Redux:无法读取未定义的属性“地图” - React-Redux: Cannot read property 'map' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM