LoginSignup
0
0

More than 3 years have passed since last update.

[React]デフォルトでオブジェクトを渡す関数における引数の受け取り方

Posted at

以下のonSortEndにおいて引数を渡したいときはどうすれば良いでしょうか。


const Sample =()=> {
 //省略
  function onSortEnd({ oldIndex, newIndex }) {
    console.log(value);
  }

  render() {
 //省略
    <SortableList
                items={value}
                onSortEnd={onSortEnd}
              />
  }
}

結論

やることは二つ。

  • 分割代入だからonSortEndの呼び出しでオブジェクトを渡す
  • コールバック関数の引数で引数を受け取る
function onSortEnd({ oldIndex, newIndex, category }) {
    console.log(category);
  }

<SortableList
                items={value}
                onSortEnd={({ oldIndex, newIndex }) =>
                  onSortEnd({
                    oldIndex: oldIndex,
                    newIndex: newIndex,
                    category: category,
                  })
                }
              />
0
0
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
0
0