【问题标题】:i want to display list of an array from react but it's not showing when i think i did everything right?我想显示来自反应的数组列表,但是当我认为我做的一切正确时它没有显示?
【发布时间】:2021-11-12 08:50:49
【问题描述】:

我想显示来自 react 的数组列表,但是当我认为我做的一切正确时它没有显示?请问我在哪里错了

下面是代码

import { useState } from 'react';

function App() {
   
  const [items , setItems] = useState([{
       id:1,
       text:'simple 1'
  },
  {
    id:1,
       text:'simple 2'
  },
  {
    id:1,
    text:'simple 3'
  },

])

  return (
    <div className="App">
    
    {items.map((item) => (
      <h1>{items.text}</h1>
    ))}


     <h1>Hello from React js</h1>
    </div>
  );
}

export default App;

【问题讨论】:

  • &lt;h1&gt;{items.text}&lt;/h1&gt; 应该是 &lt;h1&gt;{item.text}&lt;/h1&gt;。并添加一个独特的key prop
  • 哦,谢谢你,我完全忘记了

标签: arrays reactjs arraylist react-hooks


【解决方案1】:

因为您使用的是 items.text 而不是 item.text

{items.map((item) => (
  <h1>{item.text}</h1>
))}

Code 完美运行

【讨论】:

    猜你喜欢
    • 2022-09-26
    • 2022-11-19
    • 2017-05-28
    • 2021-08-13
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    • 2014-05-13
    相关资源
    最近更新 更多