简体   繁体   English

React无法读取null的属性“ id”

[英]React Cannot read property 'id' of null

This is codesandbox I want to ask about -> https://codesandbox.io/s/2pwx46572n 这是我想问的codesandbox-> https://codesandbox.io/s/2pwx46572n

When I click 'Add module' button it says 'Cannot read property 'id' of null'. 当我单击“添加模块”按钮时,它说“无法读取属性'id'为null”。 I believe the problem is in thematicArea.jsx component (components -> singleModuleComponent -> thematicArea.jsx) in line 42 (key={f.id}). 我相信问题出在第42行(key = {f.id})中的thematicArea.jsx组件(组件-> singleModuleComponent-> thematicArea.jsx)中。 It says it's null after 'Add module' click, but when I console.log it in line 32 it doesn't seem to be null. 它说单击“添加模块”后为空,但是当我在第32行进行console.log记录时,它似乎并不为空。 So...what the problem might be? 那么...可能是什么问题? Can anybody help me to fix this? 有人可以帮我解决这个问题吗?

Below thematicArea.jsx code: 下面thematicArea.jsx代码:

import React, { Component } from "react";
import SingleModule from "./singleModule";
import { Row } from "react-bootstrap";
import _ from "lodash";

class ThematicArea extends Component {
  constructor(props) {
    super(props);
    this.takeModuleNameFromSingleModule = this.takeModuleNameFromSingleModule.bind(
      this
    );
  }
  takeModuleNameFromSingleModule(value) {
    this.props.moveModuleNameUpToApp(value);
  }

  orderedModules = _.groupBy(this.props.modules, thematicArea => {
    return thematicArea.thematicArea;
  });

  render() {
    console.log(this.orderedModules);
    console.log(this.props);
    return (
      <Row>
        <div>
          {
            (this.orderedModules = Object.keys(this.orderedModules).map(e => {
              return (
                <div key={e}>
                  <h3 key={e}>{e}</h3>
                  {/*console.log(
                    Object.values(this.orderedModules[e]).map(f => f.name + f.id)
                  )*/}
                  {Object.values(this.orderedModules[e]).map(f => (
                    <SingleModule
                      moveModuleNameUpToThematicArea={
                        this.takeModuleNameFromSingleModule
                      }
                      clickedModuleNames={this.props.clickedModuleNames}
                      chosenModulesNames={this.props.chosenModulesNames}
                      key={f.id}
                      {...f}
                    />
                  ))}
                </div>
              );
            }))
          }
        </div>
      </Row>
    );
  }
}

export default ThematicArea;

singleModule.jsx (here module is displayed) singleModule.jsx(此处显示模块)

import React, { Component } from "react";
import ShowModuleDetails from "./showModuleDetails";
import AddModule from "./addModule";
import { Col } from "react-bootstrap";

class SingleModule extends Component {
  constructor(props) {
    super(props);
    this.takeModuleNameFromAddModule = this.takeModuleNameFromAddModule.bind(
      this
    );
  }
  takeModuleNameFromAddModule(value) {
    this.props.moveModuleNameUpToThematicArea(value);
  }

  render() {
    return (
      <Col
        xs={12}
        md={6}
        lg={3}
        className={
          this.props.chosenModulesNames.includes(this.props.name) === true
            ? /*"single-module col-12 col-md-6 col-lg-3 col-xl-2*/ "single-module single-module--added"
            : /*'single-module col-12 col-md-6 col-lg-3 col-xl-2*/ "single-module single-module--not-added"
        }
      >
        <h4 className="sindgle-module__thematic-area-display">
          {this.props.thematicArea}
        </h4>
        <h3 className="single-module__module-name-display">
          {this.props.name}
        </h3>
        <div className="single-module__buttons">
          <ShowModuleDetails
            details={this.props.details}
            name={this.props.name}
          />

          <AddModule
            moveModuleNameUpToSingleModule={this.takeModuleNameFromAddModule}
            isAdded={this.isAdded}
            addNewModuleToList={this.handleAddNewModuleToList}
            name={this.props.name}
            clickedModuleNames={this.props.clickedModuleNames}
            chosenModulesNames={this.props.chosenModulesNames}
          />
        </div>
      </Col>
    );
  }
}

export default SingleModule;

And addModule.jsx (shows what happens after adding a module) 和addModule.jsx(显示添加模块后发生的情况)

import React, { Component } from "react";
import { Button } from "react-bootstrap";

class AddModule extends Component {
  constructor(props) {
    super(props);
    this.handleAddModuleToList = this.handleAddModuleToList.bind(this);
  }

  handleAddModuleToList() {
    this.props.moveModuleNameUpToSingleModule(this.props.name);
  }

  isButtonDisabled = (chosenModulesNames, name) =>
    chosenModulesNames.includes(name);

  render() {
    const { chosenModulesNames, name } = this.props;
    return (
      <div>
        <Button
          disabled={
            this.props.chosenModulesNames.length === 122
              ? true
              : this.isButtonDisabled(chosenModulesNames, name)
          }
          bsStyle="primary"
          onClick={this.handleAddModuleToList}
          className={
            this.isButtonDisabled(chosenModulesNames, name) === true
              ? "single-module__buttons--chosen"
              : "single-module__buttons--not-chosen"
          }
        >
          {this.isButtonDisabled(chosenModulesNames, name) === true
            ? "Dodano!"
            : "Dodaj moduł"}
        </Button>
      </div>
    );
  }
}

export default AddModule;

Verify this.orderedModules has data when it's being rendered like: 呈现时验证this.orderedModules是否有数据:

this.orderedModules && Object.keys(this.orderedModules).map(e => {

If everything works just fine in your current code, then you may also use: 如果在当前代码中一切正常,那么您还可以使用:

key={f && f.id}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM