import { List } from 'immutable'; const myList = List([1, 2, 3]); const newList = myList.push(4); console.log(myList.toArray()); // [1, 2, 3] console.log(newList.toArray()); // [1, 2, 3, 4]
import produce from 'immer'; const myList = [1, 2, 3]; const newList = produce(myList, draft => { draft.push(4); }); console.log(myList); // [1, 2, 3] console.log(newList); // [1, 2, 3, 4]In this example, we use the Immer library to create a new immutable list. We start by creating a mutable array `myList` with the values [1, 2, 3]. We then use the `produce` function from Immer to create a new immutable list `newList`. Inside the `produce` function, we modify a draft version of the array by pushing the value `4` to the end. The original array `myList` remains unchanged.