javascript - 如何显示功能组件列表

标签 javascript reactjs react-component

我正在使用 ReactJS 创建一个基本的待办事项应用程序,我想显示待办事项列表。我将 todo 制作成一个功能组件,并创建了一个应该显示列表的 TodoList 组件。问题在于,当单击 add 按钮时,待办事项不会添加到列表中,因此列表始终为空。

这是我的 App.js:

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

const TodoForm = ({addTodo}) => {
  // Input tracker
  let input;

  return (
    <div>
      <input
    ref={node => {
      input = node;
    }}
      />
      <button
    onClick={() => {
      addTodo(input.value);
      input.value = "";
    }}
      >
    +
      </button>
    </div>
  );
};

const Todo = ({todo, remove}) => {
  // Each todo
  return <li onClick={remove(todo.id)}>{todo.text}</li>;
};

const TodoList = ({todos, remove}) => {
  // Map through the todos
  const todoNode = todos.map(todo => {
    return <Todo todo={todo} key={todo.id} remove={remove}/>;
  });
  return (
    <div className="list-group" style={{marginTop: "30px"}}>
      {todoNode}
    </div>
  );
};

const Title = () => {
  return (
    <div>
      <h1>Todo App</h1>
    </div>
  );
};

// Container component
window.id = 0;

class App extends Component {
  constructor(props) {
    // Pass props to parent class
    super(props);
    // Set initial state
    this.state = {
      data: []
    };
    this.apiUrl = ""; // my url goes here
    // Make HTTP requests with axios
    axios.get(this.apiUrl).then(res => {
      // Set state with result
      this.setState({data: res.data});
    });
  }

  // addTodo hanlder
  addTodo(val) {
    // Assemble data
    const todo = {text: val, id: window.id++};
    // Update data
    this.state.data.push(todo);
    axios.post(this.apiUrl, todo).then(res => {
      this.state.data.push(res.data);
      this.setState({data: this.state.data});
    });
  }

  render() {
    return (
      <div>
    <Title/>
    <TodoForm addTodo={this.addTodo.bind(this)}/>
    <TodoList
      todos={this.state.data}
      remove={this.handleRemove.bind(this)}
    />
      </div>
    );
  }
}

export default App;

这是我的应用程序的屏幕截图。该列表应显示在输入文本下方。

my app right now

我只收到 1 个警告,没有错误:

Line 86:  Expected to return a value at the end of arrow function  array-callback-return

我认为它在我的 addTodo 函数中的某处,但我一点也不确定。谁能帮忙?谢谢。

最佳答案

在您的 addTodohandler 中,您在 React 中使用 this.state.data.push 这不会更新状态

addTodo(val) {
    // Assemble data
    const todo = {text: val, id: window.id++};
    axios.post(this.apiUrl, todo).then(res => {
      const newData = {...this.state.data, res.data}
      this.setState({data: newData });
    });

关于javascript - 如何显示功能组件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54229077/

相关文章:

javascript - 如何摆脱 React 应用程序中的 "Functions are not valid as a React child"警告

javascript - ReactJS 渲染附加跨度 fromstring

javascript - 类型错误 : scrollIntoView is not a function

javascript - 如何在 DOJO 层中包含/添加版权文件?

node.js - 将react-scripts版本从3.4.0更新到5.0.0导致类型错误: Cannot read properties of undefined (reading 'getStackAddendum' )

javascript - 使用 ramda.js assocPath 为多个键分配特定值?

reactjs - 如何获取另一个组件的prop

javascript - 在类组件react-native中声明const

javascript - 获取用户时间并减去本地时间

javascript - node.js 请求对象-返回响应体以供进一步操作