简体   繁体   English

我已经在我的 React 网页中导入了 useState 但它仍然显示编译错误

[英]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.因为在反应组件名称应该以大写字母开头。

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.

相关问题 对显示空白网页的 Rails 做出反应,报告已更正的错误 - React on rails showing blank webpage, reporting an error that was already corrected 我尝试使用3反应导航版本运行我的drawerNavigation代码,但仍然说错误 - I have tried running my drawerNavigation code with 3 react navigation version but still says error 我想在我的应用程序中使用ng2-charts。尽管我已正确导入它,但仍然给我一个错误。找不到模块“ ng2-charts / ng2-charts” - I want to use ng2-charts in my app.though I have imported it correctly still it is giving me an error Cannot find module “ng2-charts/ng2-charts” React NativeBase没有显示我导入的组件 - React NativeBase not showing my imported components 使用 reactjs 钩子时,为什么会出现“TypeError: react__WEBPACK_IMPORTED_MODULE_1___default.a.useState is not a function”错误? - Why am I getting "TypeError: react__WEBPACK_IMPORTED_MODULE_1___default.a.useState is not a function" error when using reactjs hooks? 语法错误 - 即使我已经找到了错误,但错误仍然存在 - Sytax error - Even though I have already found the bug but the error still there 我的组件中已经有一个密钥,但为什么它仍在寻找唯一的密钥? - i already have a Key in my component but why is it still looking for a unique key? 我无法显示我网站的页面,我尝试调试未捕获的语法错误 1: import { useState } from 'react' - I cannot display a page of my website, i tried debugging Uncaught Syntax Error 1: import { useState } from 'react' 我认为我的 css 类已导入,但我已正确导入 - I think my css classes the are imported but I have imported it correctly React Hooks useState 显示错误 - React Hooks useState showing errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM