Uncaught TypeError: e.preventDefualt is not a function

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

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

  1. import { useState } from "react";
  2. export function NewTodoForm({onSubmit}) {
  3. const [newItem, setNewItem] = useState("")
  4. function handleSubmit(e) {
  5. e.preventDefault(); // This line is corrected to preventDefault
  6. if (newItem === "") return;
  7. onSubmit(newItem);
  8. setNewItem("");
  9. }
  10. return (
  11. <form className="new-item-form" onSubmit={handleSubmit}>
  12. <div className="form-row">
  13. <label htmlFor="item"> New Item</label>
  14. <input value={newItem} onChange={e => setNewItem(e.target.value)} type="text" id="item"></input>
  15. </div>
  16. <button className="btn">
  17. Add
  18. </button>
  19. </form>
  20. )
  21. }
  1. import "./styles.css";
  2. import { useState } from "react";
  3. import { NewTodoForm } from "./NewTodoForm";
  4. function App() {
  5. const [todos, setTodos] = useState([])
  6. function addTodo(title) {
  7. setTodos(currentTodos => {
  8. return [
  9. ...currentTodos,
  10. { id: crypto.randomUUID(), title, completed: false },
  11. ]
  12. })
  13. }
  14. console.log(todos)
  15. return (
  16. <>
  17. <NewTodoForm onSubmit={addTodo}/>
  18. <h1 className="header">
  19. Todo List
  20. </h1>
  21. <ul className="list">
  22. {todos.map(todo => {
  23. // eslint-disable-next-line react/jsx-key
  24. return (
  25. <>
  26. <li key={todo.id}>
  27. <label>
  28. <input type="checkbox" checked={todo.completed}/>
  29. {todo.title}
  30. </label>
  31. <button className="btn btn-danger">
  32. delete
  33. </button>
  34. </li>
  35. </>
  36. )
  37. })}
  38. </ul>
  39. </>
  40. )
  41. }
  42. 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

  1. import { useState } from &quot;react&quot;
  2. export function NewTodoForm({onSubmit}){
  3. const [newItem, setNewItem] = useState(&quot;&quot;)
  4. function handleSubmit(e) {
  5. e.preventDefualt()
  6. if (newItem === &quot;&quot;) return
  7. onSubmit(newItem)
  8. setNewItem(&quot;&quot;)
  9. }
  10. return (
  11. &lt;form className=&quot;new-item-form&quot; onSubmit={handleSubmit}&gt;
  12. &lt;div className=&quot;form-row&quot;&gt;
  13. &lt;label htmlFor=&quot;item&quot;&gt; New Item&lt;/label&gt;
  14. &lt;input value={newItem} onChange={e =&gt; setNewItem(e.target.value)} type=&quot;text&quot; id=&quot;item&quot;&gt;&lt;/input&gt;
  15. &lt;/div&gt;
  16. &lt;button className=&quot;btn&quot; &gt;
  17. Add
  18. &lt;/button&gt;
  19. &lt;/form&gt;
  20. )
  21. }
  1. import &quot;./styles.css&quot;
  2. import { useState } from &quot;react&quot;
  3. import { NewTodoForm } from &quot;./NewTodoForm&quot;
  4. function App() {
  5. const [todos, setTodos] = useState([])
  6. function addTodo(title) {
  7. setTodos(currentTodos =&gt; {
  8. return [
  9. ...currentTodos,
  10. { id: crypto.randomUUID(), title, completed: false },
  11. ]
  12. })
  13. }
  14. console.log(todos)
  15. return (
  16. &lt;&gt;
  17. &lt;NewTodoForm onSubmit={addTodo}/&gt;
  18. &lt;h1 className=&quot;header&quot;&gt;
  19. Todo List
  20. &lt;/h1&gt;
  21. &lt;ul className=&quot;list&quot;&gt;
  22. {todos.map(todo =&gt; {
  23. // eslint-disable-next-line react/jsx-key
  24. return (
  25. &lt;&gt;
  26. &lt;li key={todo.id}&gt;
  27. &lt;label&gt;
  28. &lt;input type=&quot;checkbox&quot; checked={todo.completed}/&gt;
  29. {todo.title}
  30. &lt;/label&gt;
  31. &lt;button className=&quot;btn btn-danger&quot;&gt;
  32. delete
  33. &lt;/button&gt;
  34. &lt;/li&gt;
  35. &lt;/&gt;
  36. )
  37. })}
  38. &lt;/ul&gt;
  39. &lt;/&gt;
  40. )
  41. }
  42. 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

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

  1. e.preventDefualt()

它应该是

  1. e.preventDefault()
英文:

You have a typo, your code is

  1. e.preventDefualt()

It should be

  1. 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:

确定