Better Programming

Advice for programmers.

Follow publication

Member-only story

Tree Lists With Indeterminate Checkboxes in React

Gerard van der Put
Better Programming
Published in
8 min readJan 24, 2022
What we will create today!

As always, you can find all relevant code in my GitLab repository.

Maybe you have encountered multi-leveled lists with checkboxes in the wild (see the image above). They can be found at many places and I’ve created several of them during my 20+ years as a developer.

Each item contains a checkbox and the state of one (or multiple) of those checkboxes might influence the state of other checkboxes. It’s a hierarchy and with several levels in the structure (the tree of items) things can become very complicated very quickly. Especially if you want your logic to be dynamic, and ready for the future.

So let’s jump straight into it! I’ll explain all the bits and pieces along the way. We’ll talk about the following topics:

  • Recursion
  • Checkbox component
  • List Items
  • Item State
  • Tree component
  • CheckboxList component
  • updateItemStates function
Animation: our application in action.

Recursion

Before we look at the first component a very quick word upfront. If you’re not familiar with rendering React components recursively, or recursion in general, I highly encourage you to learn a bit about it before reading on. It’s quite essential. One of my other articles might be interesting:

Checkbox component

First things first: We need a relatively simple checkbox component. In abstract terms, it’s a small component (a…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Gerard van der Put
Gerard van der Put

Written by Gerard van der Put

Lead developer for a large industry-leading tech company. Support me! https://gerardvanderput.medium.com/membership.

Responses (3)

Write a response