This site runs best with JavaScript enabled.

When to use Control Props or State Reducers

June 11, 2018

Video Blogger

someone


Comparing two similar patterns that enable many of the same use cases.

You’ve probably used components or elements that implement the control props pattern. For example:

1<input value={this.state.inputValue} onChange={this.handleInputChange} />

Read more about the concept of control props in the react docs.

You may not have had much experience with the idea of a state reducer. In contrast to control props, built-in react elements don’t support state reducers (though I hear that reason-react does). My library downshift supports a state reducer. Here’s an example of using it to prevent the menu from closing after an item is selected:

1function stateReducer(state, changes) {
2 if (changes.type === Downshift.stateChangeTypes.clickItem) {
3 // when the user clicks an item, prevent
4 // keep the isOpen to true
5 return {...changes, isOpen: true}
6 }
7 return changes
8}
9const ui = (
10 <Downshift stateReducer={stateReducer}>
11 {() => <div>{/* some ui stuff */}</div>}
12 </Downshift>
13)

You can learn how to implement these patterns from my Advanced React Component Patterns material.

Both of these patterns help you expose state management to component consumers and while they have significantly different APIs, they allow much of the same capabilities. So today I’d like to answer the question I’ve gotten many times which is: “When should I expose a state reducer or a control prop?”

Control Props are objectively more powerful because they allow complete control over state from outside the component. Let’s take my favorite Toggle component as an example:

1class Example extends React.Component {
2 state = {on: false, inputValue: 'off'}
3 handleToggle = on => {
4 this.setState({on, inputValue: on ? 'on' : 'off'})
5 }
6 handleChange = ({target: {value}}) => {
7 if (value === 'on') {
8 this.setState({on: true})
9 } else if (value === 'off') {
10 this.setState({on: false})
11 }
12 this.setState({inputValue: value})
13 }
14 render() {
15 const {on} = this.state
16 return (
17 <div>
18 {/*
19 here we're using the `value` control prop
20 exposed by the <input /> component
21 */}
22 <input value={this.state.inputValue} onChange={this.handleChange} />
23 {/*
24 here we're using the `on` control prop
25 exposed by the <Toggle /> component.
26 */}
27 <Toggle on={on} onToggle={this.handleToggle} />
28 </div>
29 )
30 }
31}

Here’s a rendered version of this component:

gif of the rendered component showing an input and toggle that sync their state

As you can see, I can control the state of the toggle button by changing the text of the input component, and control the state of the input by clicking on the toggle. This is powerful because it allows me to have complete control over the state of these components.

Control props do come with a cost however. They require that the consumer completely manage state themselves which means the consumer must have a class component with state and change handlers to update that state.

State reducers do not have to manage the component’s state themselves (though they can manage some of their own state as needed). Here’s an example of using a state reducer:

1class Example extends React.Component {
2 initialState = {timesClicked: 0}
3 state = this.initialState
4 handleToggle = (...args) => {
5 this.setState(({timesClicked}) => ({
6 timesClicked: timesClicked + 1,
7 }))
8 }
9 handleReset = (...args) => {
10 this.setState(this.initialState)
11 }
12 toggleStateReducer = (state, changes) => {
13 if (this.state.timesClicked >= 4) {
14 return {...changes, on: false}
15 }
16 return changes
17 }
18 render() {
19 const {timesClicked} = this.state
20 return (
21 <div>
22 <Toggle
23 stateReducer={this.toggleStateReducer}
24 onToggle={this.handleToggle}
25 onReset={this.handleReset}
26 />
27 {timesClicked > 4 ? (
28 <div>
29 Whoa, you clicked too much!
30 <br />
31 </div>
32 ) : (
33 <div>Click count: {timesClicked}</div>
34 )}
35 </div>
36 )
37 }
38}

And here’s a gif of the rendered interaction.

gif of the rendered component showing a toggle, reset button, and counter that’s limited to 4 toggles.

Now, you could definitely implement this experience using a control prop, but I would argue that it’s a fair bit simpler if you can use the state reducer. The biggest limitation of a state reducer is that it’s impossible to set state of the component from outside it’s normal setState calls (I couldn't implement the first example using a state reducer).

I hope this is helpful! Feel free to see the implementation and play around with things in this codesandbox.

Good luck!

Learn more about React from me:

Things to not miss:

Discuss on TwitterEdit post on GitHub

Share article
loading relevant upcoming workshops...
Kent C. Dodds

Kent C. Dodds is a JavaScript software engineer and teacher. He's taught hundreds of thousands of people how to make the world a better place with quality software development tools and practices. He lives with his wife and four kids in Utah.