我想在按下“Aggregar”按钮时在控制台中显示我的todo,但我得到以下错误:未捕获的类型错误:无法读取未定义的属性(阅读'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>
)
}
据我所知,描述从来没有作为一个属性到达表单,因为当使用.length时,它不返回任何数字,所以这就是为什么属性不满足,但我真的不知道该怎么办
1条答案
按热度按时间dffbzjpn1#
是否从
useForm
返回description
?如下定义
useForm
可以解决这个问题。