[英]I have already imported useState in my React webpage but it is still showing compilation error
I have already imported the useState but it is still showing compilation error.我已经导入了 useState 但它仍然显示编译错误。
The name of my web page is CreatePost.js我的 web 页面的名称是 CreatePost.js
import React, { useState } from 'react';
const createPost = () => {
/* We will use the 'useState' hooks to get the value from the text fields. */
const [title, setTitle] = useState("");
const [body, setBody] = useState("");
const [image, setImage] = useState("");
const postDetails = () => {
const data = new FormData();
data.append("file", image);
data.append("upload_preset", "insta-clone");
data.append("cloud_name", "rishavsinghh-cloud");
fetch("https://api.cloudinary.com/v1_1/rishavsinghh-cloud/image/upload", {
method: "post",
body: data
}).then(res => res.json()).then(data => {
console.log(data);
}).catch(err => {
console.log(err);
});
}
return (
<div className="card auth-card input-field" style={{ maxWidth:"500px", padding: "20px" }}>
<h2 className="cardHeading">Create Post</h2>
<input type="text" placeholder="Title" value={title} onChange={(event) => setTitle(event.target.value)} />
<input type="text" placeholder="Body" value={body} onChange={(event) => setBody(event.target.value)} />
<div className="file-field input-field">
<div className="btn">
<span>Upload Image <i class="fa fa-upload"></i></span>
<input type="file" value={image} onChange={(event) => setImage(event.target.files[0])} />
</div>
<div className="file-path-wrapper">
<input className="file-path validate" type="text" placeholder="Upload image" />
</div>
</div>
<button class="btn waves-effect waves-light #64b5f6 blue darken-1" onClick={ () => postDetails() } >Create Post <i class="fa fa-pencil"></i></button>
</div>
);
}
export default createPost;
The error I am getting is this:-我得到的错误是:-
Just Change your component name from createPost
to CreatePost
.只需将您的组件名称从
createPost
更改为CreatePost
。 Because in react component name should start with Upper case letter.因为在反应组件名称应该以大写字母开头。
change component name capital, like this CreatePost
更改组件名称大写,例如
CreatePost
Capitalize the Component Name大写组件名称
const CreatePost = () => {
//everything else
}
export default CreatePost
Capitalization of component names is a requirement in React so you need to change createPost
to CreatePost
and the error should be fixed.组件名称的大写是 React 中的要求,因此您需要将
createPost
更改为CreatePost
并且应该修复错误。
More info from the ReactJs Docs: ( https://reactjs.org/docs/components-and-props.html )来自 ReactJs 文档的更多信息:( https://reactjs.org/docs/components-and-props.html )
React treats components starting with lowercase letters as DOM tags.
React 将以小写字母开头的组件视为 DOM 标签。 For example, represents an HTML div tag, but represents a component and requires Welcome to be in scope.
例如,代表一个 HTML div 标签,但代表一个组件,需要 Welcome 进入 scope。
This compilation error is due to the capitalization of the component name.此编译错误是由于组件名称的大写。 Rename the component name from
createPost
to CreatePost
.将组件名称从
createPost
重命名为CreatePost
。 Also, make sure that your filename should have the same format as the component name.此外,请确保您的文件名应与组件名称具有相同的格式。
For Example:例如:
CreatePost.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.