javascript - 在反应的打字稿中简单重复调用数组在类似 3 例的 1 例中不起作用
问题描述
您好,我正在使用 react.js/typescript 编写简单的待办事项应用程序。我坚持反复调用 todos
我可以在 1) 和 3) 打电话给 todo,但我不能打电话给 2)。
有什么不同。也许我的界面类型不好todo.type.tsx
?
<TodoListItem todo={todos[0]} />
{todos.map(todo=> { <TodoListItem todo={todo} /> })}
{todos.map(todo => (<li>{todo.text} - {todo.email2}</li> ))}
还是我的 Props2 调用todo.tsx
不好?
React.FC<Props2> = ({ todo })
这是显示
==== ( localhost:9000 rendering shows this ) ======
text1"[shown in TodoListItem]"contato@gabrielrufino.com
text1 - contato@gabrielrufino.com
text2 - darthvader@starwars.com
src/pages/index.tsx
import { TodoListItem } from '../components/todo';
export default function IndexPage(){
const [todos, setTods] = useState<Todo[]>([
{
text: 'todo1',
email2: 'contato@gabrielrufino.com'
},
{
text: "todo2",
email2: 'darthvader@starwars.com'
}
])
return (
<Layout>
<TodoListItem todo={todos[0]} /> // ★I can call (★1)
<ul>
{todos.map(todo=> {
<TodoListItem todo={todo} /> // ★I CANT call (★2)
})}
</ul>
<ul>
{todos.map(todo => (
<li>{todo.text} - {todo.email2}</li> // ★I can call (★3)
))}
</ul>
</Layout>
)
}
src/model/todo.type.tsx
interface Todo {
text: string;
email2: string;
}
interface Props2 {
todo: Todo;
}
src/components/todo.tsx
import React, { FC } from 'react';
export const TodoListItem: React.FC<Props2> = ({ todo }) => {
return (
<li>
{todo.text}
<br> "shown in TodoListItem"
</br>
{todo.email2}
</li>
);
};
解决方案
问题是它根本没有返回。
尝试
<ul>
{todos.map((todo: Todo) => (
<TodoListItem todo={todo} />
))}
</ul>
或者
<ul>
{todos.map(todo=> {
return <TodoListItem todo={todo} />
})}
</ul>
代替
<ul>
{todos.map(todo=> {
<TodoListItem todo={todo} /> // ★I CANT call (★2)
})}
</ul>
推荐阅读
- jenkins - Jenkins管道得到NotSerializableException:使用sh代码时的WorkflowJob
- python - 性能优化(python):加速 .append() 到 pandas DataFrame
- r - 从函数返回列表对象
- java - 取消注册 TYPE_ORIENTATION 传感器
- python - 删除目录中的文件时找不到错误,Python
- c# - 我怎样才能收到我的服务正在被卸载的通知?
- tornado - Tornado 端点匹配
- c# - Selenium Click 在 Windows10 IE11 中不起作用
- css - 我如何为 javafx 表格列单元格应用 css
- fortran - 英特尔编译器返回“预期语句结束时找到 Format_element”