LoginSignup
1
1

More than 1 year has passed since last update.

React入門 - Tips1 - アロー関数

Last updated at Posted at 2021-04-29

目次

React入門

概要

アローとは、英語でarrow。つまり矢印ですね。
これまでのfunctionの書き方とのメリデメについては言及しません。。

これまでの書き方は、下記の1と2みたいな感じでしょうか。

// function書き方1
function test(x) {
  return x + 1;
} 

// function書き方2
const test = function(x) {
    return x + 1;
}

アロー関数での書き方

// arrow function  引数なし (かっこがいる)
const test = () => 1; // 出力の式が一行の場合はreturnは不要
// こういうことか
const test = () => {
  return 1;
} 

// arrow function 引数1つ (かっこいらない)
const test = x => x + 1; 
// これもこういうことですね。
const test = (x) => {
  return x + 1;
} 

// arrow function 引数2つ以上 (かっこいる)
const test = (x, y) => x + y + 1;
// これも書いておきます。
const test = (x, y) => {
  return x + y + 1;
}

まとめ

  • 先頭で変数を定義する(const(定数)かlet(変数))
  • () => {} の形で書く
  • 引数が1つなら()も不要 (引数0なら()は必要)
  • 出力が1行なら{}も不要
  • 出力が1行ならreturnも不要

※アロー関数で書くのが今は主流のようです。

1
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
1
1