首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法读取未定义的属性使用Map in React

无法读取未定义的属性使用Map in React
EN

Stack Overflow用户
提问于 2022-07-07 03:53:41
回答 1查看 95关注 0票数 0

我正在尝试用json格式从模拟数据中填充数据。当我试图运行组件时,我会得到未定义的读取地图错误。我已经检查了我的组件是否只使用该值进行呈现。甚至它用我得到的这个未定义的错误的值来呈现。我尝试过应用条件呈现,但这对我没有帮助。当我试图在createCells上为行应用条件呈现时,没有定义“引用错误”单元格。"CreateCellsForRow“产生了一个问题,我需要检查它是否得到一个值,如果没有值,我需要返回null。有人能帮我做错事吗。提前谢谢。

代码语言:javascript
复制
const createCell = cell => ({ key: cell.key, children: cell.title });

//Actual code
const createCellsForRow = cells => cells.map(cell => createCell(cell)); -----> I am getting error from here(undefined reading map)

//applied conditional Rendering(getting cells is not defined reference error)
const createCellsForRow = cells.length > 0 ? cells => cells.map(cell => createCell(cell)) : null

const StTable = () => {
  const [selectedKey, setSelectedKey] = useState([]);

  const handleRowToggle = (event, metaData) => {
    event.preventDefault();
    if (selectedKey !== metaData.key) {
      setSelectedKey(metaData.key);
    }
  };

  const createRow = rowData => (
    {
      key: rowData.key,
      cells: createCellsForRow(rowData.cells),
      toggleAction: {
        metaData: { key: rowData.key },
        onToggle: handleRowToggle,
        isToggled: selectedKey === rowData.key,
        toggleLabel: rowData.toggleText,
      },
    }
  );

  const createRows = data => data.map(childItem => createRow(childItem));

  return (
    <Table
      summaryId="example-single-select"
      summary="This table shows an implementation of single row selection."
      numberOfColumns={4}
      cellPaddingStyle="standard"
      rowStyle="toggle"
      dividerStyle="horizontal"
      headerData={{
        selectAllColumn: {
          checkLabel: 'Single Selection',
        },
        cells: [
          { key: 'cell-0', id: 'toggle-0', children: 'Name' },
          { key: 'cell-1', id: 'toggle-1', children: 'Address' },
          { key: 'cell-2', id: 'toggle-2', children: 'Phone Number' },
          { key: 'cell-3', id: 'toggle-3', children: 'Email Id' },
        ],
      }}
      bodyData={[
        {
          rows: createRows(mockData),
        },
      ]}
    />
  );
};

export default StTable;

//MockDataSample
[
{"SNO":001, "SregID":"SOO1", "Status": "Available"},
{"SNO":002, "SregID":"SOO2", "Status": "Not Available"},
{"SNO":003, "SregID":"SOO3", "Status": "Available"},
]
EN

回答 1

Stack Overflow用户

发布于 2022-07-07 05:44:38

您需要在每个映射函数之前设置array && array.length>0条件。array检查数组是否可用。(is数组不可用,然后未定义)

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

https://stackoverflow.com/questions/72892099

复制
相关文章

相似问题

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