首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >数组不是在映射函数中在表中呈现,而是在console.logs中呈现

数组不是在映射函数中在表中呈现,而是在console.logs中呈现
EN

Stack Overflow用户
提问于 2022-02-11 20:19:55
回答 1查看 503关注 0票数 0

我正在获取从子组件传回的personnel数组数据。当我在尝试呈现之前尝试console.log(personnel)时,它会很好地打印它,但是当我试图通过使用map函数在表中呈现它时,它不会给我任何好处。我甚至不能在map函数中控制日志。

这里是我的父组件,我希望在表中呈现personnel数组。

代码语言:javascript
运行
复制
function AllPersonnel(props) {

  let personnel = []
  let cols = [
    { path: "first_name", name: "First Name" },
    { path: "last_name", name: "Last Name" },
    { path: "section", name: "Section" },
    { path: "role", name: "Role" },
    { path: "employee_type", name: "Employee Type" },
  ];

  const handleCallback = (data) => {
    personnel.push(data)
  };
 
  return (
    <div>
      <h1>Personnel</h1>
      <div>
        {props.personnel.edges.map(({ node }) => {
          return (
            <Personnel
              key={node.__id}
              personnel={node}
              parentCallback={handleCallback}
            />
          );
        })}
      </div>

      <TableContainer component={Paper}>
        <Table>
          <TableHead>
            <TableRow>
              {cols.map((col) => {
                return <TableCell key={col.path}>{col.name}</TableCell>
              })}
            </TableRow>
          </TableHead>

          <TableBody>
            {console.log(personnel)} // this prints fine. will attach screenshot

            {personnel.map((row, i) => {
              {console.log("HERE")} // this does not print. I can't get into this map statement and I see no errors
              return (
                
                <TableRow key={row.id}>
                  
                  {console.log(row.first_name)}
                  <TableCell >
                    {row.first_name}
                  </TableCell>
                  <TableCell >{row.last_name}</TableCell>
                  <TableCell >{row.section}</TableCell>
                  <TableCell >{row.role}</TableCell>
                  <TableCell >{row.employee_type}</TableCell>
                </TableRow>
              );
            })}
          </TableBody>
        </Table>
      </TableContainer>
    </div>
  );
}

export default createFragmentContainer(AllPersonnel, {
  personnel: graphql`
    fragment AllPersonnel_personnel on PersonnelConnection {
      edges {
        node {
          ...Personnel_personnel
        }
      }
    }
  `,
});

当我尝试使用useState钩子而不是数组时,它会无限地呈现并给出错误。

超过

最大更新深度。当组件在componentWillUpdate或componentDidUpdate中反复调用componentDidUpdate时,就会发生这种情况。React限制嵌套更新的数量,以防止无限循环。

这是我的孩子部件。这很简单,一次只发送回一行,然后添加到父组件中的人事对象中。

代码语言:javascript
运行
复制
function Personnel(props) {
  const data = [
    {
      id: props.personnel.id,
      first_name: props.personnel.first_name,
      last_name: props.personnel.last_name,
      section: props.personnel.section,
      role: props.personnel.role,
    },
  ];

  props.parentCallback(data); // sends back one row at a time, 252 times in this case

  return (
    <div></div>
  );
}

export default createFragmentContainer(Personnel, {
  personnel: graphql`
    fragment Personnel_personnel on Personnel {
      id
      first_name
      last_name
      section
      role
      employee_type
      PersonnelFromResource {
        position
        notes
      }
      PersonnelFromEngagement {
        cover_term
        sector
      }
    }
  `,
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-11 22:08:16

人员是一个静态数组,组件在更改时不会重新呈现.要使组件重新呈现,必须使其成为状态。

因此,您从react import React, {useState} from 'react;导入import React, {useState} from 'react;,然后将let personnel = []更改为const [personnel , setPersonnel] = useState([]);,然后在回调中,而不是personnel.push(data)中,将其更改为setPersonnel((prevData)=>[...prevData , data])

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71086116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档