Support Control Props for all state

InstructorKent C. Dodds

Share this video with your friends

Send Tweet

Our current implementation of control props only supports controlling the state of a single item of state. Let's make our solution more generic to support any and all state of our component using Object.entries.

Karl Purkhardt
~ 6 years ago

Hi Kent. Is there any reason you've used an if/else and mutated the object here over using object spread, a ternary and computed properties? Seems cleaner to use the latter:

getState() {
    return Object.entries(this.state).reduce(
      (combinedState, [key, value]) => ({
        ...combinedState,
        [key]: this.isControlled(key) ? this.props[key] : value
      }),
      {}
    )
  }

I believe they're functionally equivalent, and appreciate the former is easier to digest if you're not used to these language features, but this an advanced course and you've not shied away from these features in previous episodes so I was curious.

Kent C. Doddsinstructor
~ 6 years ago

I honestly hadn't considered that 💯