Created
January 19, 2017 10:27
-
-
Save oleg-am/ffcf0108acc91f7f1cbfadb54442b78b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const setValue = (state, key, value, obj = false) => { | |
if (Array.isArray(key) && key.length > 1) { | |
const keys = [...key]; | |
const currentKey = keys.shift(); | |
return { | |
[currentKey]: { | |
...state[currentKey], | |
...setValue(state[currentKey], keys, value, true), | |
}, | |
}; | |
} | |
if (obj) { | |
return { | |
[key[0]]: value, | |
}; | |
} | |
return { | |
[key]: value, | |
}; | |
}; | |
// use: | |
// const actionCreator = (key, value) => ({ | |
// type: types.SET_VALUE, | |
// key, | |
// value, | |
// }); | |
// const initialState = { | |
// key1: { | |
// key2: { | |
// keyN: 'value' | |
// } | |
// } | |
// } | |
// const reducer = (state = initialState, action) => { | |
// switch (action.type) { | |
// case types.SET_VALUE: | |
// return { | |
// ...state, | |
// ...setValue(state, action.key, action.value), | |
// }; | |
// default: | |
// return state; | |
// } | |
// }; | |
// const key = ['key1', 'key2', 'keyN'] | |
// const value = 'next value' | |
// actionCreator(key, value) | |
// result: | |
// reducer = { | |
// key1: { | |
// key2: { | |
// keyN: 'next value' | |
// } | |
// } | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment