无法显示数组项目和标志错误:未介绍的TypeError:无法读取未定义的属性(Reading' MAP')

发布于 2025-02-12 17:22:30 字数 1485 浏览 0 评论 0原文

我是一个仍在调情reactjs的新鸟。为什么每当我尝试显示任务的数组内容时,我都会遇到此错误。 这是我的app.js

import React from 'react';
import { useState } from 'react'
import './index.css';
import Navbar from './components/layout/Navbar';
import Tasks from './components/Tasks';
// import Message from './components/Message';
// import FunctionClick from './components/FunctionClick';

const App = () => {
  const [tasks, setTasks] = useState([
    {
      id: 1,
      text: 'This is the first text',
      day: 'Feb 5th at 2:30pm',
      reminder: true,
    },
    {
      id: 2,
      text: 'Meeting at School',
      day: 'Feb 6th at 1:30pm',
      reminder: true,
    },
    {
      id: 3,
      text: 'third meeting',
      day: 'Feb 6th at 1:30pm',
      reminder: true,
    },
  ])
 
  return (
    <div className="container">
      <Navbar />
      <Tasks tasks={tasks}/>
    </div>
  
  );

}

export default App;

,这是我的

import React from 'react'
import  Task  from './Task'

const Tasks = ({tasks}) => {
  
  return (
    <>
    {tasks.map((task) => (
    <Task key={task.id} task={task} />
    ))}
    </>
  )
}

export default Tasks

任务

import React from 'react'

export const Task = ({task}) => {
  return (
    <div className='task'>
        <h3>{task.text}
        </h3>
    </div>
  )
}

export default Task;

。我一直被困。请帮我。您可以尝试在应用程序上测试它。谢谢

i'm a newbee still flirting around reactjs. why do i get this error anytime i trying displaying the array content of tasks.
this is my App.js

import React from 'react';
import { useState } from 'react'
import './index.css';
import Navbar from './components/layout/Navbar';
import Tasks from './components/Tasks';
// import Message from './components/Message';
// import FunctionClick from './components/FunctionClick';

const App = () => {
  const [tasks, setTasks] = useState([
    {
      id: 1,
      text: 'This is the first text',
      day: 'Feb 5th at 2:30pm',
      reminder: true,
    },
    {
      id: 2,
      text: 'Meeting at School',
      day: 'Feb 6th at 1:30pm',
      reminder: true,
    },
    {
      id: 3,
      text: 'third meeting',
      day: 'Feb 6th at 1:30pm',
      reminder: true,
    },
  ])
 
  return (
    <div className="container">
      <Navbar />
      <Tasks tasks={tasks}/>
    </div>
  
  );

}

export default App;

and this is my Tasks.js

import React from 'react'
import  Task  from './Task'

const Tasks = ({tasks}) => {
  
  return (
    <>
    {tasks.map((task) => (
    <Task key={task.id} task={task} />
    ))}
    </>
  )
}

export default Tasks

Task.js

import React from 'react'

export const Task = ({task}) => {
  return (
    <div className='task'>
        <h3>{task.text}
        </h3>
    </div>
  )
}

export default Task;

it doesn't show error in the console but shows in the browser. i've been stuck. help me please. you can try testing it on your app. thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静谧幽蓝 2025-02-19 17:22:30

我认为,根据您的错误

Uncaught TypeError: Cannot read properties of undefined (reading 'map')

,如果您进行更改,

const Tasks = ({tasks}) => {
  if(tasks && tasks.length > 0) {
    return (
      <>
      {tasks.map((task) => (
        <Task key={task.id} task={task} />
       ))}
       </>
     )
    }
   else return null;
}

则可以跳过该错误,就像您在任务中的条件组件中放置的那样,要检查任务 array

I think as per your error

Uncaught TypeError: Cannot read properties of undefined (reading 'map')

if you make changes as following

const Tasks = ({tasks}) => {
  if(tasks && tasks.length > 0) {
    return (
      <>
      {tasks.map((task) => (
        <Task key={task.id} task={task} />
       ))}
       </>
     )
    }
   else return null;
}

then you can skip that error, As you can see I put if condition inside Tasks component to check tasks array

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文