immutability

Why not to use splice with spread operator to remove item from an array in react?

馋奶兔 提交于 2021-02-20 19:11:45
问题 splice() mutates the original array and should be avoided. Instead, one good option is to use filter() which creates a new array so does not mutates the state. But I used to remove items from an array using splice() with spread operator. removeItem = index => { const items = [...this.state.items]; items.splice(index, 1); this.setState({ items }); } So in this case when I log items changes but this.state.items stays unchanged. Question is, why does everyone use filter instead of splice with

Why not to use splice with spread operator to remove item from an array in react?

心不动则不痛 提交于 2021-02-20 19:11:20
问题 splice() mutates the original array and should be avoided. Instead, one good option is to use filter() which creates a new array so does not mutates the state. But I used to remove items from an array using splice() with spread operator. removeItem = index => { const items = [...this.state.items]; items.splice(index, 1); this.setState({ items }); } So in this case when I log items changes but this.state.items stays unchanged. Question is, why does everyone use filter instead of splice with

Recursively remove nullish values from a JavaScript object

匆匆过客 提交于 2021-02-19 07:39:10
问题 I searched for this but couldn't find a satisfactory answer so I'm posting my own answer here. Basically I wanted a function that: takes an object as its argument recursively removes properties whose values are null , undefined , [] , {} or '' retains 0 and false values returns a new object with those properties removed preferably in a functional style without mutations 回答1: Here's what I came up with. (Thanks to Nina for providing a sample ;) const is_obj = x => x !== null && typeof x ===

Updating state - why creating a new copy of state when calling setState?

六月ゝ 毕业季﹏ 提交于 2021-02-11 01:12:16
问题 React docs: Never mutate this.state directly, as calling setState() afterwards may replace the mutation you made. Treat this.state as if it were immutable. That's clear. class App extends React.Component { state = { data: [] } the following I understand updateState(event) { const {name, value} = event.target; let user = this.state.user; // this is a reference, not a copy... user[name] = value; // return this.setState({user}); // so this could replace the previous mutation } this following I

Difference in mutability between reference and box

拟墨画扇 提交于 2021-02-10 12:33:51
问题 I'm trying to understand Rust pointer types and their relation to mutability. Specifically, the ways of declaring a variable which holds the pointer and is itself mutable -- i.e. can be pointed to some other memory, and declaring that the data itself is mutable -- i.e. can be changed through the value of the pointer variable. This is how I understand plain references work: let mut a = &5; // a is a mutable pointer to immutable data let b = &mut 5; // b is an immutable pointer to mutable data

Update multiple values in a sequence

空扰寡人 提交于 2021-02-08 05:47:44
问题 To get a sequence with one value updated, one can use seq.updated(index, value) I want to set a new value for a range of elements. Is there a library function for that? I currently use the following function: def updatedSlice[A](seq: List[A], ind: Iterable[Int], value: A): List[A] = if (ind.isEmpty) seq else updatedSlice(seq.updated(ind.head, value), ind.tail, value) Besides the need of writing function, this seems to be inefficient, and also works only for lists, rather than arbitrary

Updating state from recursively rendered component in React.js

寵の児 提交于 2021-02-08 05:22:50
问题 I am in need of updating deeply nested object in a React state from a recursively rendered component. The items look like this and can be nested dynamically: const items = [ { id: "1", name: "Item 1", isChecked: true, children: [] }, { id: "3", name: "Item 3", isChecked: false, children: [ { id: "3.1", name: "Child 1", isChecked: false, children: [ { id: "3.1.1", name: "Grandchild 1", isChecked: true, children: [] }, { id: "3.1.2", name: "Grandchild 2", isChecked: true, children: [] } ] }, {

Updating state from recursively rendered component in React.js

徘徊边缘 提交于 2021-02-08 05:22:06
问题 I am in need of updating deeply nested object in a React state from a recursively rendered component. The items look like this and can be nested dynamically: const items = [ { id: "1", name: "Item 1", isChecked: true, children: [] }, { id: "3", name: "Item 3", isChecked: false, children: [ { id: "3.1", name: "Child 1", isChecked: false, children: [ { id: "3.1.1", name: "Grandchild 1", isChecked: true, children: [] }, { id: "3.1.2", name: "Grandchild 2", isChecked: true, children: [] } ] }, {

Js Array.prototype.map() happens to be mutable?

一曲冷凌霜 提交于 2021-02-07 12:54:05
问题 Why would the map method mutate the original array when its initial purpose is to create a new array ? I have an array of object which I pass to a pure function which in turn maps the given array and return a new one. Then I notice that the original array was also changed.. I understand the concept that Object in Js are passed by reference and all but still cant quite grab why would the implementation of map would mutate the original array, kinda beats the purpose IMO. var initialArray = [ {

How Immutability is Implemented

我是研究僧i 提交于 2021-02-07 11:45:08
问题 I am trying to grasp how the trie and such in immutability is implemented, as relates to immutability in JS. I understand how there is supposed to be significant structural sharing. My question is say you have a graph sort of structure like this: a -- b | c | d -- h | e -- i -- l | f -- j -- m | g -- k -- n So then you add an x to the system. I'll try it two different ways: a -- b | c | d -- h -- x | e -- i -- l | f -- j -- m | g -- k -- n That one is just added as a leaf node. a -- b | c | d