Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for How to create a function component in react
Aastha Pandey
Aastha Pandey

Posted on

     

How to create a function component in react

Below are the two ways of creating function component.

importReactfrom"react";functionFirstComponent(){return(<div><h1>Helloreact!!!</h1></div>);}exportdefaultFirstComponent
Enter fullscreen modeExit fullscreen mode

or
The below code is usingarrow function for creating function component.

importReactfrom"react";constFirstComponent=()=>{return(<div><h1>Helloreact!!!</h1></div>);}exportdefaultFirstComponent
Enter fullscreen modeExit fullscreen mode

The above two function components can be importedwithout enclosing the component name inside curly braces becausedefault has been used withexport.

Note:There can be only one default export per module.

export default FirstComponent

import FirstComponent from "FirstComponent"

But if there is onlyexport in front of a function component then the import requires curly braces around the component's name.

export const FirstComponent = () => {}

import {FirstComponent} from "FirstComponent"

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I write only on those topics, wherein I myself was stuck at some point in time.
  • Work
    Front end developer
  • Joined

More fromAastha Pandey

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp