3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

React JSXの書き始め方

Last updated at Posted at 2021-01-05

Reactを勉強していると、
JSXでいろんな書き方が出てきて混乱してしまう。
そもそもJavascriptが苦手なので、よくわからない。
そんな私が未来の自分(またよくわからなくなった時の自分)へ宛てた記事です。


import React from "react";
export default function (){
    return(
        <div>
            <h1>あいうえお</h1>
        </div>
    )
}

import React from "react";
const Kansu1 = function (){
      return(
        <div>
            <h1>あいうえお</h1>
        </div>
    )
 }
 export default Kansu1


import React from "react";
const Kansu2 = ()=>{
    return(
        <div>
            <h1>あいうえお</h1>
        </div>
    )
}
export default Kansu2

上の3つは関数の名前は違うけれど全部同じ動きをする。
これがよくわからなかったので、整理していく。

JSXの流れ

  • returnするだけの関数を作る
  • 表示したい内容をreturnの中身に入れる。
  • 関数をexport defaultする

これが基本!絶対のルール!:hand_splayed_tone2::hand_splayed_tone2::hand_splayed_tone2:
惑わされないで:bangbang:

改めて、最初の3つについて確認すると、

①は関数を直接export defaultしているだけ。

②③は定数にreturnする関数を代入。定数をexport defaultしている。

③は②をアロー関数を使って書いただけ。

アロー関数を忘れた人:point_down_tone2::point_down_tone2::point_down_tone2:

普通の関数の定義 


const 定数名 = function(){処理};

アロー関数 

function()の代わりに()=>で関数を定義できる。 


const 定数名 = ()=>{処理}
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?