简体   繁体   English

我没有在我的 React-app 中获得列表项

[英]I am not getting the list item in my React-app

I am new in React.我是 React 的新手。 I just created a React-app named TodoList.我刚刚创建了一个名为 TodoList 的 React 应用程序。 In my src/mycomponents, I have four files Header.js, Todos.js, TodoItem.js and Footer.js.在我的 src/mycomponents 中,我有四个文件 Header.js、Todos.js、TodoItem.js 和 Footer.js。 I made some todolist and trying to show on display.我做了一些 todolist 并试图展示。 But I am not getting any item.但我没有得到任何物品。 I am not getting any error.我没有收到任何错误。 The Only problem I am facing is that I am not getting any list item.我面临的唯一问题是我没有得到任何列表项。

My App.js我的App.js

import "./App.css";
import Header from "./MyComponents/Header";
import {Todos} from "./MyComponents/Todos";
import Footer from "./MyComponents/Footer";
import {TodoItem} from "./MyComponents/TodoItem";

function App() {
  let todos = [
    {
      sno:1,
      title: "Go to the Market",
      desc: "You need to go to market to get this job done"

    },
    {
      sno:2,
      title: "Go to the Market",
      desc: "You need to go to market to get this job done"

    },
    {
      sno:3,
      title: "Go to the Market",
      desc: "You need to go to market to get this job done"

    }
  ]
  return (
    <>
      <Header title="My Todos List"/>
      <Todos todos={Todos}/>
      
      
    </>
  );
}

export default App;

''' '''

My Todos.js我的Todos.js

import React from 'react'
import {TodoItem} from "../MyComponents/TodoItem";


export const  Todos = (props) =>{
    return (
        <div className="container">
            <h3> Todos List</h3>
            <TodoItem todo={props.todos[0]}/>
        </div>
    ) 
}

My TodoItem.js我的TodoItem.js

import React from 'react'

export const TodoItem = (todos) => {
    return (
        <div>
            <h4>{todos.title}</h4>
            <p>{todos.desc}</p>

        </div>
    )
}

Tell me if code of any other file requires.告诉我是否需要任何其他文件的代码。 I have attached the screenshot.我附上了截图。 Thank you in advance.先感谢您。 在此处输入图像描述

You have couple of typing mistakes in your code.您的代码中有几个输入错误。

  1. You are passing capital Todos instead of todos您正在传递资本 Todos 而不是todos
<Todos todos={todos}/>  // pass correct todos list here
  1. In your TodoItem file you need to extract props correctly在您的TodoItem文件中,您需要正确提取道具
import React from 'react'

export const TodoItem = ({todo}) => {
    return (
        <div>
            <h4>{todo.title}</h4>
            <p>{todo.desc}</p>
        </div>
    )
}

Note: Since you are passing first todo item from Todos file, this will render only one todo item.注意:由于您从Todos文件传递第一个 todo 项,因此这将只呈现一个 todo 项。 If you want to render all todo list you need to change code like below:-如果要呈现所有待办事项列表,则需要更改如下代码:-

Todos.js

import React from 'react'
import {TodoItem} from "../MyComponents/TodoItem";


export const  Todos = ({todos}) =>{
    return (
        <div className="container">
            <h3> Todos List</h3>
            <TodoItem todos={todos}/>
        </div>
    ) 
}

TodoItem.js

import React from 'react'

export const TodoItem = ({todos}) => {
    return (
        <div>
            {todos.map(todo => (
              <h4>{todo.title}</h4>
              <p>{todo.desc}</p>
            ))}
        </div>
    )
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 第一次使用 React-app 时出现错误,错误是 core-js-pure@3.6.5 postinstall - I am getting error while using React-app first time, Error is core-js-pure@3.6.5 postinstall 创建 React 应用程序失败。 我使用了 npm init react-app my-app - Creating React app failed. I used npm init react-app my-app 列表中的每个孩子都应该有一个唯一的“关键”道具,我在我的反应原生应用程序中遇到了这个错误 - Each child in a list should have a unique "key" prop, I am getting this error in my react native app 如何通过 npm 启动我的 react-app? (缺少启动脚本?) - How can i start my react-app via npm ? (start-script missing?) 我是否需要安装 babel 才能在我的 react-app 中编写 ES6 代码? - Do I need to install babel to be able to write ES6 code in my react-app? 为什么我无法在 google、yahoo 和 bing 搜索中索引我的 react-app? - Why I can't index my react-app on google,yahoo and bing searches? 我可以将 arguments 提供给 react-app 吗? (React 的依赖注入) - Can I give arguments to a react-app? (dependency injection with React) 为什么录像不能在我的React-app中工作? - Why isn't video recording working in my React-app? 我正在尝试从 React 中的输入将列表项添加到有序列表中,但第一项在第一次加载后被添加了两次 - I am trying to add a list item to an ordered list from an input in React , but the first item is getting added twice after the first loading 我正在尝试通过 express 为我的 React 应用程序提供服务,为什么我会收到 404? - I'm trying to serve my react app from express, why am I getting a 404?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM