【问题标题】:props is undefined in react js when used in the child class在子类中使用时,props 在 React js 中未定义
【发布时间】:2020-03-16 11:21:36
【问题描述】:

我试图从父类向子类发送一个值,但由于某种原因我得到了未定义的道具有什么帮助吗? 这是父类并将数据发送给子类。 我已按要求添加了完整的子代码。我不确定如何添加道具,因为它是一个函数,当我尝试在其中添加道具时,我仍然收到道具未定义错误

{this.state.viewProfile ? (
                <SideProfileDrawer
                  people={this.state.people}
                  viewprof={this.state.viewProfile}
                />
              ) : null}

这是我在孩子中使用道具

    import { makeStyles } from "@material-ui/core/styles";
import InstructionsModal from "./instructionsmodal";
import List from "@material-ui/core/List";
import Divider from "@material-ui/core/Divider";
import React, { Component } from "react";
import Tooltip from "@material-ui/core/Tooltip";
import Drawer from "@material-ui/core/Drawer";
import Zoom from "@material-ui/core/Zoom";
import UserProfile from "../quiz/userProfile";
import { Button } from "react-bootstrap";
import { render } from "react-dom";
import Test from "../quiz/test";

export default function TemporaryDrawer(props) {
  const useStyles = makeStyles({
    list: {
      width: 680
    },
    fullList: {
      width: "auto"
    }
  });
  const classes = useStyles();
  const [state, setState] = React.useState({
    top: false,
    left: false,
    bottom: false,
    right: false
  });

  const toggleDrawer = (side, open) => event => {
    if (
      event.type === "keydown" &&
      (event.key === "Tab" || event.key === "Shift")
    ) {
      return;
    }

    setState({ ...state, [side]: open });
  };

  const sideList = side => (
    <div
      className={classes.list}
      role="presentation"
      onClick={toggleDrawer(side, false)}
      onKeyDown={toggleDrawer(side, false)}
    >
      {this.props.people.map((person, index) => {
        return (
          <UserProfile
            className="userProfile"
            levelRook={person.levelRook}
            levelStudent={person.levelStudent}
            levelIntermediate={person.levelIntermediate}
            levelExpert={person.levelExpert}
            levelMaster={person.levelMaster}
            score={person.Score}
            question={person.Questions}
            email={person.email}
            time={person.lastLogin}
          />
        );
      })}
    </div>
  );

  return (
    <div>
      {this.props.viewprof?sideList:null}
      <Button onClick={toggleDrawer("right", true)}>Open Right</Button>

      <Drawer
        anchor="right"
        open={state.right}
        onClose={toggleDrawer("right", false)}
      >
        {sideList("right")}
      </Drawer>
    </div>
  );
}

任何解决这个问题的帮助我都试过了

【问题讨论】:

  • 请分享您的子组件的完整代码
  • 你的组件不是类组件,而是功能组件。您可以使用side.people 而不是this.props.people 访问它
  • @Chris 会访问我从父级发送的数据吗?
  • @Chris 一旦我完成了我得到的地图功能是未定义的
  • 哦,我现在明白了。试试props,没有this

标签: reactjs


【解决方案1】:

不要使用 this.props 只需使用 props 因为你有无状态组件而不是有状态 read more

【讨论】:

  • 与组件的状态无关。
  • 不,我的意思是子组件不是一个类,它的功能组件和有状态和无状态是一个旧术语来表示这个
  • 我不是你链接的文章的忠实粉丝。它是重叠的状态和类/函数组件,它们不是一回事
  • 是的,因为在 react 钩子之前,拥有状态的唯一方法是使用类组件。现在你可以使用钩子,就像 OP 实际使用的那样。
  • @ArnaudClaudel medium.com/@cgcrutch18/… 看看这个可能你觉得有用
猜你喜欢
  • 2017-12-04
  • 1970-01-01
  • 2021-07-18
  • 2020-10-16
  • 2017-03-30
  • 2020-11-25
  • 2022-01-17
  • 1970-01-01
  • 2021-09-13
相关资源
最近更新 更多