Skip to content

Commit

Permalink
inherited state, create if does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoloza committed Jul 13, 2023
1 parent 88a525a commit 3aee101
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/state/inherit.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ export const getChildStateInKey = (stateKey, parentState, options = {}) => {
for (let i = 0; i < arrLength; i++) {
const childKey = arr[i]
const grandChildKey = arr[i + 1]
const childInParent = parentState[childKey]
if (childInParent && childInParent[grandChildKey]) {
stateKey = grandChildKey
parentState = childInParent
} else return

let childInParent = parentState[childKey]
if (!childInParent) childInParent = parentState[childKey] = {}
if (!childInParent[grandChildKey]) childInParent[grandChildKey] = {}

stateKey = grandChildKey
parentState = childInParent
}
if (options.returnParent) return parentState
return parentState[stateKey]
Expand Down

0 comments on commit 3aee101

Please sign in to comment.