title: Function to update object values with different nesting depths
body: <p>This may be a duplicate as I believe it's a common situation but I can't find anything on Stack Overflow that answers my question.</p>
  
  <p>I have set an object that holds my initial state as follows (extract to show only relevant values for this question):</p>
  
  <pre><code>export const initialState = {
    signUpDialog: {
      open: false,
      nameField: {
        isEmpty: false,
        isInvalid: false
    }
  }
  </code></pre>
  
  <p>As you can see, the <code>open</code> state is nested 1 deep under signUpDialog however <code>isEmpty</code> and <code>isInvalid</code> are nested 2 deep.</p>
  
  <p>I am using React Hooks to manage state, and have the following action creator to pass values to my state reducer:</p>
  
  <pre><code>export const TOGGLE_BOOLEAN = "TOGGLE_BOOLEAN";
  export function toggleBoolean(state, subState, value) {
    return {
      type: TOGGLE_BOOLEAN,
      payload: {
        state: state,
        subState: subState,
        value: value
      }
    };
  }
  </code></pre>
  
  <p>And the following reducer that changes the state (note: the <code>state</code> param is equal to the current state and the <code>action</code> param is equal to the return value of the <code>toggleBoolean</code> action creator):</p>
  
  <pre><code>const reducer = (state, action) =&gt; {
    const reduced = { ...state };
    switch (action.type) {
      case TOGGLE_BOOLEAN:
        return Object.assign({}, state, {
          ...reduced,
          [action.payload.state]: {
            ...reduced[action.payload.state],
            [action.payload.subState]: action.payload.value
          }
        });
      default:
        state;
    }
  };
  </code></pre>
  
  <p>This works fine for updating the <code>open</code> state as it's only nested 1 deep under action.payload.state='signUpForm', however it won't work for <code>name: { isEmpty}</code> or <code>name: { isInvalid }</code> as those are both nested 2 deep.</p>
  
  <p>I know I could solve this by having different action creators and switch cases for each level of depth, however it wouldn't be DRY as they are essentially doing the same thing.</p>
  
  <p>Are there any simpler DRY solutions to this?</p>