简体   繁体   English

React Hooks Todo App 'TypeError: Object(...) is not a function' 错误

[英]React Hooks Todo App 'TypeError: Object(...) is not a function' error

I'm developing a Todo App with React Hooks.我正在使用 React Hooks 开发一个 Todo 应用程序。 I get the error "TypeError: Object (...) is not a function" when defining the functional component.定义功能组件时出现错误“TypeError: Object (...) is not a function”

React version: 16.7.0反应版本:16.7.0

Here is the error:这是错误:

  1 | import React, { useState } from "react"
  2 | import "./App.css"
  3 | 
> 4 | export default function TodoApp() {
  5 |   const [todo, setTodo] = useState("")
  6 |   const [todos, setTodos] = useState([])
  7 | 

And my code:还有我的代码:

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

export default function TodoApp() {
  const [todo, setTodo] = useState("")
  const [todos, setTodos] = useState([])

  const handleChange = (e) => {
    setTodo(e.target.value)
  }

  const addTodo = () => {
    setTodos([
      ...todos,
      {
        id: todos.length + 1,
        title: todo,
        isDone: false
      }
    ])
  }

  continued...

You seem to have a mistake inside the addTodo function.您似乎在addTodo函数中有错误。 If your current state depends on the previous state you have to pass a function instead to setTodos to avoid bugs.如果您的当前状态依赖于先前的状态,则您必须将一个函数传递给setTodos以避免错误。 Try this:尝试这个:

const addTodo = () => {
    setTodos(prevTodos => [
      ...prevTodos,
      {
        id: prevTodos.length + 1,
        title: todo,
        isDone: false
      }
    ])
  }

暂无
暂无

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

相关问题 反应钩子:'TypeError:Object 不是函数' - React hooks: 'TypeError: Object is not a function' 我收到错误消息:```TypeError: Object(...) is not a function``` in my react app- 第一次尝试使用钩子 - I'm getting the error: ```TypeError: Object(…) is not a function``` in my react app- trying to use hooks for the first time React 和 hooks - 获取 TypeError: Object(...) is not a function - React and hooks - Getting TypeError: Object(...) is not a function Error creating an delete function in a Todo app Redux: TypeError: state.byIds.filter is not a function - Error creating an delete function in a Todo app Redux : TypeError: state.byIds.filter is not a function TypeError:todo不是函数 - TypeError: todo is not a function TypeError:无法读取未定义的属性'isCompleted'-React Simple Todo应用 - TypeError: Cannot read property 'isCompleted' of undefined - React simple todo app React Hooks: Uncaught TypeError: setData({...}) is not a function - React Hooks: Uncaught TypeError: setData({...}) is not a function 类型错误:setCartCount 不是 function - React Hooks - Redux - TypeError: setCartCount is not a function - React Hooks - Redux Ruby on Rails with React“TypeError: Object(...) is not a function”错误 - Ruby on Rails with React "TypeError: Object(...) is not a function" Error TypeError: Object(...) 在使用 useHistory() 钩子时不是函数 - TypeError: Object(...) is not a function when using useHistory() hooks
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM