Back to blog list

Don't use useState to handle arrays in React

Pavitra GolchhaPublished on Feb 17, 2022

    reacthooksarrayfrontend

Problem 😣

To keep a state of primitive values, React's built-in useState hook is ideal. But, for arrays and objects?... It is quite not intuitive nor effective.

Cause ⁉️

The skills required to use spread operator ... to manipulate an array is out of the world. Spread operator is also known to be a key thing that slow things down. It has an O(n) runtime performance which gets hidden behind a cute syntax. But, when it is used too much in ways it was not designed to be used, things gets really messy and it hurts readabilty.

1function Old() {
2  const [items, setItems] = useState([])
3
4  function push() {
5    setItems([...items, `${items.length}`])
6  }
7
8  function pop() {
9    items.pop()
10    setItems([...items])
11  }
12
13  function pushLeft() {
14    setItems([randomValue(), ...items])
15  }
16
17  function popLeft() {
18    const [v, ...el] = items
19    setItems(el)
20  }
21
22  function insert() {
23    items.splice(randomIndex(items), 0, randomValue())
24    setItems([...items])
25  }
26
27  function update() {
28    items[randomIndex(items)] = randomValue()
29    setItems([...items])
30  }
31
32  function remove() {
33    items.splice(randomIndex(items), 1)
34    setItems([...items])
35  }
36
37  function clear() {
38    setItems([])
39  }
40
41  return (
42    <div className="space-y-4">
43      <h1><b>Old Way</b></h1>
44      <div className="flex flex-row flex-wrap gap-2">
45        <Button onClick={push}>Push</Button>
46        <Button onClick={pop}>Pop</Button>
47        <Button onClick={pushLeft}>Push Left</Button>
48        <Button onClick={popLeft}>Pop Left</Button>
49        <Button onClick={insert}>Insert</Button>
50        <Button onClick={update}>Update</Button>
51        <Button onClick={remove}>Remove</Button>
52        <Button onClick={clear}>Clear</Button>
53      </div>
54      <div className="flex flex-row gap-2 bg-slate-200 p-2 h-12">
55        {items.map((item, i) => (
56          <Item key={i}>{item}</Item>
57        ))}
58      </div>
59    </div>
60  )
61}

A common pattern we can notice here is that the array is first manipulated and then, to cause a re-render, the array is cloned before setting the state. However, this can lead to big performance issue as cloning an array is not cheap, especially when it contains larger objects in larger quantity.

Solution 💡

I came up with an idea of writing my own hook to handle arrays efficiently. This is inspired by Jetpack Compose's val myList = remember { mutableStateListOf<T>() } (thing!). We can easily manipulate the list like how we do in an imperative fashion. This will trigger recompositions whenever an item is updated, added or removed.

Here's an example of what I mean

1@Composable
2fun SomeComponent() {
3  val list = remember { mutableStateListOf<Int>() }
4
5  LaunchedEffect(Unit) {
6    // manipulate list here
7    list.add(123)
8  }
9
10  // just use the list interface anywhere
11  list.forEach { item ->
12    Text(item)
13  }
14}

Without further ado, let's jump right into the code which I implemented.

1export function useArrayState(initial = []) {
2  const array = useMemo(() => initial, [])
3  const [refresh, setRefresh] = useState(0)
4  const cb = useCallback((f) => {
5    f(array)
6    setRefresh(it => ++it)
7  }, [])
8
9  return [array, cb]
10}

This hook allows us to rewrite the <Old> component like so:

1function New() {
2  const [items, updateItems] = useArrayState([56, 98, 36])
3
4  function push() {
5    updateItems(it => it.push(it.length))
6  }
7
8  function pop() {
9    updateItems(it => it.pop())
10  }
11
12  function pushLeft() {
13    updateItems(it => it.unshift(randomValue()))
14  }
15
16  function popLeft() {
17    updateItems(it => it.shift())
18  }
19
20  function insert() {
21    updateItems(it => it.splice(randomIndex(it), 0, randomValue()))
22  }
23
24  function update() {
25    updateItems(it => it[randomIndex(it)] = randomValue())
26  }
27
28  function remove() {
29    updateItems(it => it.splice(randomIndex(it), 1))
30  }
31
32  function clear() {
33    updateItems(it => it.length = 0)
34  }
35
36  return (
37    <div className="space-y-4">
38      <h1><b>New Way</b></h1>
39      <div className="flex flex-row flex-wrap gap-2">
40        <Button onClick={push}>Push</Button>
41        <Button onClick={pop}>Pop</Button>
42        <Button onClick={pushLeft}>Push Left</Button>
43        <Button onClick={popLeft}>Pop Left</Button>
44        <Button onClick={insert}>Insert</Button>
45        <Button onClick={update}>Update</Button>
46        <Button onClick={remove}>Remove</Button>
47        <Button onClick={clear}>Clear</Button>
48      </div>
49      <div className="flex flex-row gap-2 bg-slate-200 p-2 h-12">
50        {items.map((item, i) => (
51          <Item key={i}>{item}</Item>
52        ))}
53      </div>
54    </div>
55  )
56}

Try it out here: https://replit.com/@pavi2410/React-useArrayState

Similar hooks

This hook is from https://mantine.dev which I didn't know about until midway of writing this 🥲. If I knew already, you wouldn't be here reading this. Anyway, here it is: https://mantine.dev/hooks/use-list-state/

Final Words

Declarative UI pattern in blooming and changing how we design and develop UIs. It feels great in every way. All we need is to use these cool stuffs responsively and efficiently such that it doesn't affect readability, performance and hurt peoples' brains.

Comments? Feedback?

I'd love to hear you -> Blog: Don't use useState to handle arrays in React