简体   繁体   English

REACT-Popover 不显示我的 json 内容

[英]REACT- Popover doesn't display my json content

In menu/ the name of my invited people are not diplayed there is only the InfoIcon in the Cell.menu/我邀请的人的名字没有显示,单元格中只有InfoIcon I want to create a Popover , when you click on the InfoIcon , you get all the info of the invited people(name and location).我想创建一个Popover ,当您单击InfoIcon时,您将获得受邀人员的所有信息(姓名和位置)。

export default function Display() {
  const { dishes } = JsonData;
  const [anchor, setAnchor] = useState(null);
  const openPopover = (event) => {
    setAnchor(event.currentTarget);
  };

  const data = useMemo(
    () => [
     ...
      {
        //Problem: participants not displayed and click not working
        Header: "Invited",
        id: "invited",
        accessor: (row) => row.invited.map(({ name }) => name).join(", "),
        Cell: (props) => (
          <div>
            <InfoIcon />
            <Popover
              open={Boolean(anchor)}
              anchorEl={anchor}
              anchorOrigin={{
                vertical: "top",
                horizontal: "left"
              }}
              transformOrigin={{
                vertical: "bottom",
                horizontal: "right"
              }}
            >
              <Typography variant="h1">{props.participants}</Typography>
            </Popover>
          </div>
        )
      },
    ],
    []
  );

  return (
    <Table
      data={dishes}
      columns={data}         
    />
  );
}

Here is my code这是我的代码

In addition to saving the clicked element into state so the Popover component has an element ref it needs to also store in state which specific row's participants to render into the popover.除了将单击的元素保存到 state 中, Popover组件还有一个元素 ref,它还需要在 state 中存储要渲染到 popover 中的特定行的参与者。 Currently the code is using a singular boolean value for all the popovers.目前,代码对所有弹出框都使用了一个单一的布尔值。 Use the row.id to open a specific popover.使用row.id打开特定的弹出框。

Don't forget to add the "anchor" state to the dependency array so the popover gets the latest state.不要忘记将“锚”状态添加到依赖数组中,以便弹出框获得最新状态。

function Display() {
  const { menus } = JsonData;

  const [anchorId, setAnchorId] = useState(null);
  const [anchorEl, setAnchorEl] = useState(null);

  const openPopover = id => (event) => {
    setAnchorId(id);
    setAnchorEl(event.currentTarget);
  };

  const handleClose = () => {
    setAnchorId(null);
    setAnchorEl(null);
  };

  const data = useMemo(
    () => [
      {
        Header: "Id",
        accessor: (row) => row.id
      },
      {
        Header: "Invited",
        id: "invited",
        accessor: (row) => row.invited,
        Cell: (props) => (
          <div>
            {props.value.map(({ name }) => name).join(", ")}
            <InfoIcon onClick={openPopover(props.row.id)} />
            <Popover
              open={anchorId === props.row.id}
              onClose={handleClose}
              anchorEl={anchorEl}
              anchorOrigin={{
                vertical: "top",
                horizontal: "left"
              }}
              transformOrigin={{
                vertical: "bottom",
                horizontal: "right"
              }}
            >
              <Typography variant="h6">
                {props.value.map(({ name, location }) => (
                  <div key={name}>
                    <p>{name}</p>
                    <p>Location: {location}</p>
                  </div>
                ))}
              </Typography>
            </Popover>
          </div>
        )
      },
      {
        Header: "Title",
        accessor: (row) => ({ title: row.title, id: row.id }),
        Cell: ({ value }) => (
          <Link to={{ pathname: `/menu/${value.id}` }}>{value.title}</Link>
        )
      }
    ],
    [anchorEl, anchorId]
  );

  const initialState = {
    sortBy: [
      { desc: false, id: "id" },
      { desc: false, id: "invited" },
      { desc: false, id: "title" }
    ]
  };

  return (
    <Table
      data={menus}
      columns={data}
      initialState={initialState}
      withCellBorder
      withRowBorder
      withSorting
      withPagination
    />
  );
}

编辑 react-popover-doesnt-display-my-json-content

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

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