javascript - TypeError: Object(...) is not a function (React.JS)
问题描述
I was watching a crud full stack tutorial in react and this error appeared on my browser
TodoForm.js
import React, { useForm, useHistory, useEffect, setTodo } from 'react';
export const TodoForm = ({ todo, onSubmit }) => {
const {register, handleSubmit} = useForm({ defaultValues: { text: todo ? todo.text : ""} });
const history = useHistory()
useEffect(() => {
setTodo({
text: "foo"
})
}, [])
const submitHandler = handleSubmit((data) => {
onSubmit(data)
history.push("/");
})
return (
<form onSubmit={submitHandler}>
<div className="form-group">
<label htmlFor="text">Text:</label>
<input className="form-control" ref={register} type="text" name="text" id="text">
</input>
</div>
<div className="form-group">
<button className="btn btn-primary">
CreateTodo
</button>
</div>
</form>
);
}
CreateTodo.js
import React from 'react';
import { TodoForm } from './TodoForm'
export const CreateTodo = () => {
const onSubmit = (data) => {
alert(JSON.stringify(data))
}
return (
<div className="container" >
<div className="mt-3">
<h3>Create Todo Item</h3>
<TodoForm onSubmit={onSubmit} />
</div>
</div>
);
};
I already updated my React, the version is 17.0.1. When I removed this line <TodoForm onSubmit={onSubmit} />
it was worked. In my browser screen, seems that the error is on the files above. What should I do?
解决方案
useForm doesn't seem to belong to "react", the one most close is "react-hook-form", probably you should install that module and import from it, not "react".
推荐阅读
- emacs - 在 Org 模式下导出为 PDF 时如何对齐源代码块?
- javascript - 将 html 表格数据转为在 javascript 循环中运行
- swift - 调用 stopActivityUpdates 不会停止更新
- python - 你如何简化这些循环?
- c++ - 使用 getline 通过传入智能指针参数来读取文本
- python - 序列化和反序列化 oauth2client.client.OAuth2Credentials
- javascript - 如何在 JS 画布中更改鼠标功能以触摸
- cmake - 如何制作可以使用 FIND_PACKAGE 找到的 CMake 项目?
- html - 无法从 npm 模块内联字体
- java - 使用 CipherOutputStream 进行递归加密会给出一个空字节 []