moment.js
日付管理ライブラリ
2020/9頃にメンテナンスモードに入っている ref 新規プロジェクトへの導入は推奨されていない
ビルドサイズが大きく、代替するLibraryが他に出てきたのが主な要因
validation
isValid
sortしたい
code:ts
const sorted = _.orderBy(contests, c => moment(c.createdAt), 'desc') 注意
以下のようにすると、monthが1つずれる
code:js
moment({
year: 2000,
month: 1,
day: 1
}).format('YYYY-MM-DD')
// -> "2000-02-01"
^
めっちゃ注意
momentはinstanceを持ち回るので、useStateでmomentをstateにするときは注意が必要 ref 以下のようにして、stateであるmomentオブジェクトをReduxに渡して、
そのRedux処理の中でdate.add(1, 'months')のような処理を行うと、Component内のlocal stateであるはずのdateも更新されてしまう
code:ts
dispatch(hogeAction(date));
通常では考えられない挙動なのでバグ特定の為に時間がかかった
解決策はdeep cloneをする
code:ts
dispatch(hogeAction(date.clone()));
型でも検出できず、actionを呼ぶ時に忘れたらバグるので難易度が高い
理想的には、momentの使用自体をヤメたいmrsekut.icon
webpackでmomentの不要なロケールを取り除く
moment-locales-webpack-plugin
momentjsがでかいのをどうにかする
code:js
webpack: (config, _options) => {
config.plugins.push(new MomentLocalesPlugin())
return config
}