Form.File 正在调用错误 React.jsx:类型无效 - 需要字符串或类/函数(对于复合组件)但得到:未定义
我正在尝试使用与 multer 进行反应将图像上传到网站。在这个过程中我陷入困境,因为 Form.File 未定义,我不知道如何克服这个问题。
这是 ProductEditScreen.js
import React, { useState, useEffect } from "react";
import { Form, Button } from "react-bootstrap";
import FormContainer from "../components/FormContainer";
const ProductEditScreen = () => {
const [image, setImage] = useState("");
const [uploading, setUploading] = useState(false);
const uploadFileHandler = async (e) => {
const file = e.target.files[0];
const formData = new FormData();
formData.append("image", file);
setUploading(true);
};
const submitHandler = (e) => {
console.log("action");
};
return (
<>
<FormContainer>
<Form onSubmit={submitHandler}>
<Form.Group controlId="image">
<Form.Label>Image</Form.Label>
<Form.Control
type="text"
placeholder="Enter image url"
value={image}
onChange={(e) => setImage(e.target.value)}
></Form.Control>
<Form.File
id="image-file"
label="Choose File"
custom
onChange={uploadFileHandler}
></Form.File>
</Form.Group>
<Button type="submit" variant="primary">
Update
</Button>
</Form>
</FormContainer>
</>
);
};
export default ProductEditScreen;
并且此 ProductEditScreen 在 App.js 中使用,因为
import ProductEditScreen from "./screens/ProductEditScreen";
const App = () => {
return (
<BrowserRouter>
<Header />
<main className="py-3">
<Container>
<Routes>
<Route
path="/admin/product/:id/edit"
element={<ProductEditScreen />}
/>
</Routes>
</Container>
</main>
<Footer />
</BrowserRouter>
);
};
控制台中显示的错误是:
Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
Check your code at ProductEditScreen.js:30.
at ProductEditScreen (http://localhost:3000/static/js/bundle.js:5892:76)
at Routes (http://localhost:3000/static/js/bundle.js:60546:5)
at div
at http://localhost:3000/static/js/bundle.js:27569:5
at main
at Router (http://localhost:3000/static/js/bundle.js:60479:15)
at BrowserRouter (http://localhost:3000/static/js/bundle.js:59955:5)
at App
at Provider (http://localhost:3000/static/js/bundle.js:57170:20)
react-dom.development.js:25058
Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
Check the render method of `ProductEditScreen`.
at createFiberFromTypeAndProps (react-dom.development.js:25058:1)
at createFiberFromElement (react-dom.development.js:25086:1)
at createChild (react-dom.development.js:13446:1)
at reconcileChildrenArray (react-dom.development.js:13719:1)
at reconcileChildFibers (react-dom.development.js:14125:1)
at reconcileChildren (react-dom.development.js:16990:1)
at updateHostComponent (react-dom.development.js:17632:1)
at beginWork (react-dom.development.js:19080:1)
at HTMLUnknownElement.callCallback (react-dom.development.js:3945:1)
at Object.invokeGuardedCallbackDev (react-dom.development.js:3994:1)
如何解决这个问题? 或者是否有任何其他替代方法,我可以在选择文件时调用我的方法 uploadFileHandler 。
I am trying to upload a image to website using react with multer.And in the process i am stuck because the Form.File is undefined and i dont know how to overcome this.
Here is the ProductEditScreen.js
import React, { useState, useEffect } from "react";
import { Form, Button } from "react-bootstrap";
import FormContainer from "../components/FormContainer";
const ProductEditScreen = () => {
const [image, setImage] = useState("");
const [uploading, setUploading] = useState(false);
const uploadFileHandler = async (e) => {
const file = e.target.files[0];
const formData = new FormData();
formData.append("image", file);
setUploading(true);
};
const submitHandler = (e) => {
console.log("action");
};
return (
<>
<FormContainer>
<Form onSubmit={submitHandler}>
<Form.Group controlId="image">
<Form.Label>Image</Form.Label>
<Form.Control
type="text"
placeholder="Enter image url"
value={image}
onChange={(e) => setImage(e.target.value)}
></Form.Control>
<Form.File
id="image-file"
label="Choose File"
custom
onChange={uploadFileHandler}
></Form.File>
</Form.Group>
<Button type="submit" variant="primary">
Update
</Button>
</Form>
</FormContainer>
</>
);
};
export default ProductEditScreen;
And this ProductEditScreen is used in the App.js as
import ProductEditScreen from "./screens/ProductEditScreen";
const App = () => {
return (
<BrowserRouter>
<Header />
<main className="py-3">
<Container>
<Routes>
<Route
path="/admin/product/:id/edit"
element={<ProductEditScreen />}
/>
</Routes>
</Container>
</main>
<Footer />
</BrowserRouter>
);
};
Error displayed in the console are:
Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
Check your code at ProductEditScreen.js:30.
at ProductEditScreen (http://localhost:3000/static/js/bundle.js:5892:76)
at Routes (http://localhost:3000/static/js/bundle.js:60546:5)
at div
at http://localhost:3000/static/js/bundle.js:27569:5
at main
at Router (http://localhost:3000/static/js/bundle.js:60479:15)
at BrowserRouter (http://localhost:3000/static/js/bundle.js:59955:5)
at App
at Provider (http://localhost:3000/static/js/bundle.js:57170:20)
react-dom.development.js:25058
Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
Check the render method of `ProductEditScreen`.
at createFiberFromTypeAndProps (react-dom.development.js:25058:1)
at createFiberFromElement (react-dom.development.js:25086:1)
at createChild (react-dom.development.js:13446:1)
at reconcileChildrenArray (react-dom.development.js:13719:1)
at reconcileChildFibers (react-dom.development.js:14125:1)
at reconcileChildren (react-dom.development.js:16990:1)
at updateHostComponent (react-dom.development.js:17632:1)
at beginWork (react-dom.development.js:19080:1)
at HTMLUnknownElement.callCallback (react-dom.development.js:3945:1)
at Object.invokeGuardedCallbackDev (react-dom.development.js:3994:1)
How to get over with this ?
Or is there any other alternative method where i can call my method uploadFileHandler when the file is choosed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查你的react-bootstrap版本。 React-bootstrap 版本 v2.5.0 (Bootstrap 5.2) 已删除所有文件-相关组件。
需要使用
代替。Check your react-bootstrap version. The react-bootstrap version v2.5.0 (Bootstrap 5.2) has dropped all file-related components.
Need to use
<Form.Control type="file" />
instead.