Once you've added data to your collection, you'll probably need to change it. Lists and maps each have two approaches to change existing values.
Lists can either set a value or update a value. The distinction is subtle, so let's compare the two approaches now.
When you set list values using the set()
method, you're changing an existing value. More specifically, you're overwriting the current value at a given index with a new value:
const myList = List.of(1); const myChangedList = myList.set(0, 2); console.log('myList', myList.toJS()); // -> myList [ 1 ] console.log('myChangedList', myChangedList.toJS()); // -> myChangedList [ 2 ]
You're updating the first list value—because the index you're passing to set()
is 0
—with a value of 2
. Using set()
like this is a good choice when you know ahead of time what the new value should be. But what about when the new value depends on the current value?