Javascript React-函数作为React子函数无效 类TodoList扩展了React.Component{ 建造师(道具){ 超级(道具); } passingTodos=()=>{ this.props.todos.map(项=>{ 返回; }); }; render(){ 返回( 事項清單 {this.props.todos.map(项=>{ 返回; })} ); } }

Javascript React-函数作为React子函数无效 类TodoList扩展了React.Component{ 建造师(道具){ 超级(道具); } passingTodos=()=>{ this.props.todos.map(项=>{ 返回; }); }; render(){ 返回( 事項清單 {this.props.todos.map(项=>{ 返回; })} ); } },javascript,reactjs,Javascript,Reactjs,像这样,所有的东西都会呈现,但是如果我把{this.passingTodos}放进去,没有任何东西呈现,我会在标题中得到警告。我不完全确定原因。首先您需要从passingTodos方法返回结果,然后您需要在渲染中调用它 class TodoList extends React.Component { constructor(props) { super(props); } passingTodos = () => { this.props.todos.map(

像这样,所有的东西都会呈现,但是如果我把
{this.passingTodos}
放进去,没有任何东西呈现,我会在标题中得到警告。我不完全确定原因。

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

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>
    );
  }
}
类TodoList扩展了React.Component{
建造师(道具){
超级(道具)
}
passingTodos=()=>{
返回此.props.todos.map((项)=>{
返回
})
}
render(){
返回(
事項清單
{this.passingTodos()}
)
}
}

是{this.passingTodos()}不是{this.passingTodos}谢谢我注意到我错过了第二天早上的返回:D
    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>
            )
        }
    }