Uncaught TypeError: 无法读取未定义的属性(读取’length’)在提交表单中

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

Uncaught TypeError: Cannot read properties of undefined (reading 'length') in the submit form

问题

我想在按下"Agregar"按钮时在控制台中显示我的待办事项,但我得到以下错误:Uncaught TypeError: Cannot read properties of undefined (reading 'length'),你能帮我吗? Uncaught TypeError: 无法读取未定义的属性(读取’length’)在提交表单中

TodoApp.jsx:

import { useReducer } from "react";
import { todoReducer } from "./todoReducer";
import { TodoList } from "./TodoList";
import { TodoAdd } from "./TodoAdd";

export const TodoApp = () => {
    const initialState = [
      {
        id: new Date().getTime(),
        description: "recolectar piedra de alma",
        done: false,
      },
      {
        id: new Date().getTime() * 3,
        description: "recolectar piedra de infinito",
        done: false,
      },
    ];

    const [todos, dispatchTodo] = useReducer(todoReducer, initialState);

    const handleNewTodo = (todo) => {
      console.log({ todo });
    };

    return (
      <>
        <h1>
          TodoApp (10) <small>Pendientes 2</small>
        </h1>
        <hr />

        <div className="row">
          <div className="col-7">
            <TodoList todos={todos} />
          </div>
        </div>

        <div className="col-5">
          <h4>Agregar TODO</h4>
          <hr />
          <TodoAdd onNewTodo={handleNewTodo} />
        </div>
      </>
    );
};

TodoAdd.jsx:

import { useForm } from '../hooks/useForm';

export const TodoAdd = ({ onNewTodo }) => {
    const { description, onInputChange, onResetForm } = useForm({
        description: ''
    });

    const onFormSubmit = (event) => {
        event.preventDefault();
        if (description.length <= 1) return;

        const newTodo = {
            id: new Date().getTime(),
            done: false,
            description: description,
        };

        onNewTodo(newTodo);
        onResetForm();
    }

    return (
        <form onSubmit={onFormSubmit}>
            <input
                type="text"
                placeholder="¿Qué hay que hacer?"
                className="form-control"
                name="description"
                value={description}
                onChange={onInputChange}
            />

            <button
                type="submit"
                className="btn btn-outline-primary mt-1"
            >
                Agregar
            </button>
        </form>
    )
}

从我理解的情况来看,description属性似乎从未传递给表单,因为当使用.length时不返回任何数字,所以属性没有被填充。但我真的不知道该怎么做。

英文:

I want to show my todo in the console when pressing the "Agregar" button, but I get the following error: Uncaught TypeError: Cannot read properties of undefined (reading 'length'), could you give me a hand? Uncaught TypeError: 无法读取未定义的属性(读取’length’)在提交表单中

TodoApp.jsx

import { useReducer } from &quot;react&quot;
import { todoReducer } from &quot;./todoReducer&quot;;
import { TodoList } from &quot;./TodoList&quot;;
import { TodoAdd } from &quot;./TodoAdd&quot;;


export const TodoApp = () =&gt; {
    const initialState = [
      {
        id: new Date().getTime(),
        description: &quot;recolectar piedra de alma&quot;,
        done: false,
      },
      {
        id: new Date().getTime() * 3,
        description: &quot;recolectar piedra de infinito&quot;,
        done: false,
      },
    ];
  
    const [todos, dispatchTodo] = useReducer(todoReducer, initialState);
  
    const handleNewTodo = (todo) =&gt; {
      console.log({todo});
    };
  
    return (
      &lt;&gt;
        &lt;h1&gt;
          TodoApp (10) &lt;small&gt;Pendientes 2&lt;/small&gt;
        &lt;/h1&gt;
        &lt;hr /&gt;
  
        &lt;div className=&quot;row&quot;&gt;
          &lt;div className=&quot;col-7&quot;&gt;
            &lt;TodoList todos={todos} /&gt;
          &lt;/div&gt;
        &lt;/div&gt;
  
        &lt;div className=&quot;col-5&quot;&gt;
        &lt;h4&gt;Agregar TODO&lt;/h4&gt;
        &lt;hr /&gt;
        &lt;TodoAdd onNewTodo={handleNewTodo} /&gt;
        &lt;/div&gt;
      &lt;/&gt;
    );
  };

TodoAdd.jsx:

import { useForm } from &#39;../hooks/useForm&#39;;


export const TodoAdd = ({ onNewTodo }) =&gt; {

    const { description, onInputChange, onResetForm } = useForm({
        description: &#39;&#39;
    });

    const onFormSubmit = ( event ) =&gt; {
        event.preventDefault();
        if ( description.length &lt;= 1 ) return;

        const newTodo = {
            id: new Date().getTime(),
            done: false,
            description: description,
        }

        onNewTodo(newTodo);
        onResetForm();
    }


    return (
        &lt;form onSubmit={ onFormSubmit }&gt;
            &lt;input 
                type=&quot;text&quot; 
                placeholder=&quot;&#191;Qu&#233; hay que hacer?&quot;
                className=&quot;form-control&quot;
                name=&quot;description&quot;
                value={ description }
                onChange={ onInputChange }
            /&gt;

            &lt;button 
                type=&quot;submit&quot;
                className=&quot;btn btn-outline-primary mt-1&quot;
            &gt;
                Agregar
            &lt;/button&gt;
        &lt;/form&gt;
    )
}

From what I understand, the description never arrives as a property to the form, since when using the .length it does not return any number, so that is why the property is not fulfilled, but I really don't know what to do

答案1

得分: 0

你是否从useForm中返回description?按照以下方式定义useForm可能会解决问题。

export const useForm = ({ description }) => {
  const [newDescription, setNewDescription] = useState(description);
  const onInputChange = (e) => {
    setNewDescription(e.target.value);
  };
  const onResetForm = () => {
    setNewDescription("");
  };
  return { description: newDescription, onInputChange, onResetForm };
};
英文:

Do you return description from useForm?
Define useForm as follows may solve the problem.

export const useForm = ({ description }) =&gt; {
  const [newDescription, setNewDescription] = useState(description);
  const onInputChange = (e) =&gt; {
    setNewDescription(e.target.value);
  };
  const onResetForm = () =&gt; {
    setNewDescription(&quot;&quot;);
  };
  return { description: newDescription, onInputChange, onResetForm };
};

huangapple
  • 本文由 发表于 2023年4月4日 12:12:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925460.html
匿名

发表评论

匿名网友

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

确定