【问题标题】:Map Method Returns Undefined When Using in React App [duplicate]在 React App 中使用时 Map 方法返回未定义 [重复]
【发布时间】:2020-06-16 02:28:21
【问题描述】:

我正在尝试在我的 React 应用程序中使用 map 函数并且未定义。返回(对象)的实际数组有效,但是在运行 map 函数时,它返回未定义。在 React 之外一切正常,所以我假设它是语法:

const Filter = ({ list }) => {

    const filterHandler = () => {
      const newList = list.map(item => {
        item.id;
      });
      console.log(list); //this successfully logs products array
      console.log(newList); //this logs undefined
    };

    return (

        <div>
          <button onClick={filterHandler}>Filter</button>
        </div>

    )
}

export default Filter;

【问题讨论】:

标签: javascript reactjs


【解决方案1】:

您必须指定return 关键字:

const newList = list.map(item => {
   return item.id;
});

或:在单行中不使用{}

const newList = list.map(item => item.id);

【讨论】:

  • 哎呀,菜鸟的错误
  • @EricNguyen,list 长什么样子?
猜你喜欢
  • 2021-08-16
  • 2018-03-25
  • 2018-10-28
  • 2017-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-10
  • 1970-01-01
相关资源
最近更新 更多