1.1.0 • Published 4 years ago
redux-state-editor v1.1.0
redux-state-editor
- 思いついたので冗談で作ってみたredux用のstate編集ツール
- 多分計算コストが尋常じゃないので実用性はない
使い方
インストール
npm i --save redux-state-editor
ソースファイル
typescript
- インポート
import ReduxStateEditor from 'redux-state-editor';
使い方
- 編集したい項目だけを持ったstateと同じ構造のオブジェクトを用意する
- 編集したい項目に編集したい内容のの関数を設定する
- 処理を実行する
sample
const state = { a:{ b:{ d:"hoge", e:"fuga" }, c:{ f:"foo", g:"bar" } } } const editorObject = { a: { //b: 編集しないので設定しない c: (c: { f: string; g: string; }) => { return { f: "fooooooo", g: c.g + c.g } } } nextState = ReduxStateEditor<typeof state>(editorObject, state); // nextState はこのようになる // { // "a": { // "b": { // "d": "hoge", // "e": "fuga" // }, // "c": { // "f": "fooooooo", // "g": "barbar" // } // } // } // 変更したオブジェクトの参照だけが切れる // nextState === state -> false // nextState.a === state.a -> false // nextState.a.b === state.a.b -> true // nextState.a.c === state.a.c -> false
注意事項
- 配列の一部だけを変更することはできないので、全体に対して適用するような関数が必要