Uncaught TypeError: e.preventDefualt is not a function

huangapple go评论77阅读模式
英文:

Uncaught TypeError: e.preventDefualt is not a function

问题

I'm new to React and js in general and I'm following a crash course on React and got stuck on this error at some point in the course.
This is the error message that I'm getting is for the preventDefault method, which I have used to prevent the page from refreshing.

here is the 2 file concern

import { useState } from "react";

export function NewTodoForm({onSubmit}) {
    const [newItem, setNewItem] = useState("")

    function handleSubmit(e) {
        e.preventDefault(); // This line is corrected to preventDefault
        if (newItem === "") return;
        onSubmit(newItem);
        setNewItem("");
    }

    return (
        <form className="new-item-form" onSubmit={handleSubmit}>
            <div className="form-row">
                <label htmlFor="item"> New Item</label>
                <input value={newItem} onChange={e => setNewItem(e.target.value)} type="text" id="item"></input>
            </div>
            <button className="btn">
                Add
            </button>
        </form>
    )
}
import "./styles.css";
import { useState } from "react";
import { NewTodoForm } from "./NewTodoForm";

function App() {
    const [todos, setTodos] = useState([])

    function addTodo(title) {
        setTodos(currentTodos => {
            return [
                ...currentTodos,
                { id: crypto.randomUUID(), title, completed: false },
            ]
        })
    }
    console.log(todos)
    return (
        <>
            <NewTodoForm onSubmit={addTodo}/>
            <h1 className="header">
                Todo List
            </h1>

            <ul className="list">
                {todos.map(todo => {
                    // eslint-disable-next-line react/jsx-key
                    return (
                        <>
                            <li key={todo.id}>
                                <label>
                                    <input type="checkbox"  checked={todo.completed}/>
                                    {todo.title}
                                </label>
                                <button className="btn btn-danger">
                                    delete
                                </button>
                            </li>
                        </>
                    )
                })}
            </ul>
        </>
    )
}

export default App

I tried to use other methods rather than preventDefault, but none of them works, from preventing from refreshing the page.

英文:

I'm new to React and js in general and I'm following a crash course on React and got stuck on this error at some point in the course.
This is the error message that I'm getting is for the preventDefault method, which I have used to prevent the page from refreshing.

here is the 2 file concern

import { useState } from &quot;react&quot;

export function NewTodoForm({onSubmit}){
    const [newItem, setNewItem] = useState(&quot;&quot;)
  function handleSubmit(e) {

    e.preventDefualt()
    if (newItem === &quot;&quot;) return
    onSubmit(newItem)
    
    setNewItem(&quot;&quot;)
  }
return (
    &lt;form className=&quot;new-item-form&quot; onSubmit={handleSubmit}&gt;
        &lt;div className=&quot;form-row&quot;&gt;
          &lt;label htmlFor=&quot;item&quot;&gt; New Item&lt;/label&gt;
          &lt;input value={newItem} onChange={e =&gt; setNewItem(e.target.value)} type=&quot;text&quot; id=&quot;item&quot;&gt;&lt;/input&gt;
        &lt;/div&gt;
        &lt;button className=&quot;btn&quot; &gt;
          Add
        &lt;/button&gt;
      &lt;/form&gt;
)
}

import &quot;./styles.css&quot;
import { useState } from &quot;react&quot;
import { NewTodoForm } from &quot;./NewTodoForm&quot;

function App() {


  const [todos, setTodos] = useState([])
  

  function addTodo(title) {
    setTodos(currentTodos =&gt; {
      return [
        ...currentTodos,
        { id: crypto.randomUUID(), title, completed: false },
      ]
    })
  }
  console.log(todos)
  return (
    
    &lt;&gt;
      &lt;NewTodoForm onSubmit={addTodo}/&gt;
    
      &lt;h1 className=&quot;header&quot;&gt;
        Todo List
      &lt;/h1&gt;
     
      
      &lt;ul  className=&quot;list&quot;&gt;
      {todos.map(todo =&gt; {
        // eslint-disable-next-line react/jsx-key
        return (
          &lt;&gt; 
          &lt;li key={todo.id}&gt;
          &lt;label&gt;
            &lt;input type=&quot;checkbox&quot;  checked={todo.completed}/&gt;
            {todo.title}

          &lt;/label&gt;
          &lt;button className=&quot;btn btn-danger&quot;&gt;
            delete
          &lt;/button&gt;

        &lt;/li&gt;
        &lt;/&gt;
        )
        
      })}
      
      &lt;/ul&gt;
    &lt;/&gt;



  )
}

export default App

I tried to use other methods rather than preventDefualt, but none of them works, from preventing from refreshing the page.

答案1

得分: 5

你有一个拼写错误,你的代码是

e.preventDefualt()

它应该是

e.preventDefault()
英文:

You have a typo, your code is

e.preventDefualt()

It should be

e.preventDefault()

答案2

得分: 0

问题出在handleSubmit函数中,其中e.preventDefault()被拼写错误。另外,不要忘记将您的函数导出为"default"。

英文:

The issue is in handleSubmit function where e.preventDefault() has been misspelled. Also don't forget to make "default" export of your function.

huangapple
  • 本文由 发表于 2023年6月19日 18:11:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76505629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定