简体   繁体   English

警告:函数无效,因为 React 子项不显示

[英]Warning: Functions are not valid as a React child- List items does not show up

When i compile the app i get this warning in the console:当我编译应用程序时,我在控制台中收到此警告:

Warning: Functions are not valid as a React child.警告:函数作为 React 子节点无效。 This may happen if you return a Component instead of from render.如果您返回 Component 而不是从 render 返回,则可能会发生这种情况。 Or maybe you meant to call this function rather than return it.或者,您可能打算调用此函数而不是返回它。

My App.js:我的 App.js:

import "./App.css";
import React, { Component } from "react";

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      toDoList: [],
      activeItem: {
        id: null,
        title: "",
        completed: false,
      },
      editing: false,
    };
    this.fetchTasks = this.fetchTasks.bind(this);
  }

  componentWillMount() {
    this.fetchTasks();
  }

  fetchTasks() {
    console.log("Fetching...");

    fetch("http://127.0.0.1:8000/api/task-list/")
      .then((response) => response.json())
      .then((data) =>
        this.setState({
          toDoList: data,
        })
      );
  }

  render() {
    var tasks = this.state.toDoList;

    return (
      <div className="container">
        <div id="task-container">
          <div id="form-wrapper">
            <form id="form">
              <div className="flex-wrapper">
                <div style={{ flex: 6 }}>
                  <input
                    className="form-control"
                    type="text"
                    name="title"
                    placeholder="Add task"
                  />
                </div>

                <div style={{ flex: 1 }}>
                  <input
                    className="btn btn-warning"
                    id="submit"
                    type="submit"
                    name="Add"
                  />
                </div>
              </div>
            </form>
          </div>
          <div className="list-wrapper">
            {
              (tasks.map = (task, index) => {
                return (
                  <div key="{index}" className="task-wrapper flex-wrapper">
                    <span>{task.title}</span>
                  </div>
                )})
            }
          </div>
        </div>
      </div>
    );
  }
}

export default App;

Basically i'm trying to list the items in the api list but i'm missing something.基本上我试图列出 api 列表中的项目,但我错过了一些东西。 Anyone help me with it?有人帮我吗?

(tasks.map = (task, index) => {
                return (
                  <div key="{index}" className="task-wrapper flex-wrapper">
                    <span>{task.title}</span>
                  </div>
                )})

Should be:应该:

(tasks.map(task, index) => {
                return (
                  <div key="{index}" className="task-wrapper flex-wrapper">
                    <span>{task.title}</span>
                  </div>
                )})

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM