[英]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.您的代码中有几个输入错误。
todos
您正在传递资本 Todos 而不是todos
<Todos todos={todos}/> // pass correct todos list here
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.