immutable.js

Immutable.Map.deleteAll() is not a function

让人想犯罪 __ 提交于 2019-11-29 07:37:40
Consider the following code: const person = Immutable.Map({ name: 'John', surname: 'Maverick', age: 39 }); const mutated = person.deleteAll(['name', 'age']); Expected result would be that mutated now is a new instance of Map with the keys name and age deleted. However, throws an exception: Uncaught TypeError: person.deleteAll is not a function When inspecting the available methods of Immutable.Map prototype, there is no deleteAll nor removeAll methods. Have they been removed? The method is listed in the official ImmutableJS documentation, but it is not available. What would be a native

How can I set a deeply nested value in Immutable.js?

谁都会走 提交于 2019-11-29 02:06:40
问题 When working with plain JavaScript objects it's easy to change a deeply nested object property: people.Thomas.nickname = "Mr. T"; But with Immutable I have to go through each property's ancestors before I have a new people object: var thomas = peopleImmutable.get("Thomas"); var newThomas = thomas.set("nickname", "Mr .T"); peopleImmutable = peopleImmutable.set("Thomas", newThomas); Is there a more elegant way to write this? 回答1: Maps in Immutable have a setIn method that makes it easy to set

Performance issues with a tree structure and shouldComponentUpdate in React / Redux

假如想象 提交于 2019-11-28 21:26:00
问题 I'm fairly new to React, Redux and ImmutableJS, and have run into some performance issues. I have a large tree structure of data, which I'm currently storing as a flat list in this structure: new Map({ 1: new Node({ id: 1, text: 'Root', children: [2,3] }), 2: new Node({ id: 2, text: 'Child 1', children: [4] }), 3: new Node({ id: 3, text: 'Child 2', children: [] }), 4: new Node({ id: 4, text: 'Child of child 1', children: [] }) }); While structuring it as a flat list makes updating nodes easy,

Why should I use immutablejs over object.freeze?

不问归期 提交于 2019-11-28 20:58:26
问题 I have researched on net about the benefits of immutablejs over Object.freeze() but didn't find anything satisfying! My question is why I should use this library and work with non native data structures when I can freeze a plain old javascript object? 回答1: I don't think you understood what immutablejs offers. It's not a library which just turns your objects immutable, it's a library around working with immutable values. Without simply repeating their docs and mission statement, I'll state two

How to describe Immutable.js Map shape with Flow

我的未来我决定 提交于 2019-11-28 19:07:25
I would like to describe the shape of a map using Immutable's flow type definitions. You can describe the shape of an object by: const stateShape: { id: number, isActive: boolean } = { id: 123, isActive: true }; Is there something similar for Immutable's Maps? TL;DR; No, but using Records you can get Flow to typecheck the shape but not the types. Longform The correct answer would be: no, since maps don't have shapes (at least in Flow and Immutable) . But Immutable does have a type for "Maps" with shapes. That would be Records . But for reasons described below (since it's not strictly relevant)

React performance: rendering big list with PureRenderMixin

放肆的年华 提交于 2019-11-28 18:43:17
I took a TodoList example to reflect my problem but obviously my real-world code is more complex. I have some pseudo-code like this. var Todo = React.createClass({ mixins: [PureRenderMixin], ............ } var TodosContainer = React.createClass({ mixins: [PureRenderMixin], renderTodo: function(todo) { return <Todo key={todo.id} todoData={todo} x={this.props.x} y={this.props.y} .../>; }, render: function() { var todos = this.props.todos.map(this.renderTodo) return ( <ReactCSSTransitionGroup transitionName="transition-todo"> {todos} </ReactCSSTransitionGroup>, ); } }); All my data is immutable,

React-Redux complex (deep) state objects

两盒软妹~` 提交于 2019-11-28 17:14:25
Given my initial redux state is : const state = { currentView: 'ROOMS_VIEW', navbarLinks: List([ {name: 'Rooms', key: 'ROOMS_VIEW'}, {name: 'Dev', key: ''} ]), roomListsSelected: {group: 0, item: 0}, roomLists: [ { name: "Filters", expanded: true, listItems: [ { icon: 'images/icon-warning.svg', name: 'Alerts', filter: room => room.hasAlert }, { icon: 'images/icon-playlist.svg', name: 'In Progress', filter: room => room.progress > 20 }, { icon: 'images/icon-playlist.svg', name: 'Almost Done', filter: room => room.progress > 90 }, { icon: 'images/icon-playlist.svg', name: 'Complete', filter:

What are disadvantages to using immutable state in React?

回眸只為那壹抹淺笑 提交于 2019-11-28 06:51:13
I have built my first React application with stateful stores the "normal" way, and now I am looking into using an immutable global state like used in the Este starterkit. The state of all stores is kept together in a single immutable data structure Components have no state but access data in their render() based on a store getter function Stores are also stateless but mutate the global application state for their domain using a cursor. The top level app component listens for state changes, and re-renders the whole component tree. Components are implemented as "pure" meaning they use

How to use Immutable.js with redux?

冷暖自知 提交于 2019-11-28 02:55:16
Redux framework is using reducers to change app state in response to an action. The key requirement is that a reducer cannot modify an existing state object; it must produce a new object. Bad Example : import { ACTIVATE_LOCATION } from './actions'; export let ui = (state = [], action) => { switch (action.type) { case ACTIVATE_LOCATION: state.activeLocationId = action.id; break; } return state; }; Good Example : import { ACTIVATE_LOCATION } from './actions'; export let ui = (state = [], action) => { switch (action.type) { case ACTIVATE_LOCATION: state = Object.assign({}, state, {

In ImmutableJS, how to push a new array into a Map?

☆樱花仙子☆ 提交于 2019-11-27 17:50:28
问题 How can I achieve the following using ImmutableJS: myMap.get(key).push(newData); 回答1: You can do as follows: (see this JSBin) const myMap = Immutable.fromJS({ nested: { someKey: ['hello', 'world'], }, }); const myNewMap = myMap.updateIn(['nested', 'someKey'], arr => arr.push('bye')); console.log(myNewMap.toJS()); // { // nested: { // someKey: ["hello", "world", "bye"] // } // } Since myMap is immutable, whenever you try to set/update/delete some data within it, it will return a reference to