gpt4 book ai didi

javascript - React - 函数作为 React 子项无效

转载 作者:行者123 更新时间:2023-12-03 00:25:44 25 4
gpt4 key购买 nike

class TodoList extends React.Component {
constructor(props) {
super(props);
}

passingTodos = () => {
this.props.todos.map(item => {
return <TodoItem key={item.id} todo={item} deleteTodo={this.props.deleteTodo} />;
});
};

render() {
return (
<div className="todoList">
<h1>Todo List</h1>
<div className="todo-items">
{this.props.todos.map(item => {
return <TodoItem key={item.id} todo={item} deleteTodo={this.props.deleteTodo} />;
})}
</div>
</div>
);
}
}

像这样,所有内容都会呈现,但如果我输入 {this.passingTodos} 则不会呈现任何内容,并且我会在标题中收到警告。我不太确定为什么。

最佳答案

首先您需要从passingTodos方法返回结果,其次您需要在渲染中调用它

    class TodoList extends React.Component{
constructor(props){
super(props)
}

passingTodos = () => {
return this.props.todos.map( (item) => {
return <TodoItem key={item.id} todo = {item} deleteTodo = {this.props.deleteTodo}/>
})
}

render(){
return(
<div className = "todoList">
<h1>Todo List</h1>
<div className = "todo-items">
{this.passingTodos()}
</div>
</div>
)
}
}

关于javascript - React - 函数作为 React 子项无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54103909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com