immutable.js

TypeScript | Immutable | proper way of extending Immutable.Map type

最后都变了- 提交于 2019-12-06 06:24:47
问题 I have a react-redux application written in typescript with immutable package. There I have a data, which comes from api and in store I pack it to Map. In all application they are used as a Map. I created an interface: export interface PaymentMethod extends Immutable.Map<string, string | NamedType<number>> { id: string; name: string; description: string; accountNr: string; paymentMethodType: NamedType<number>; } In general it works very good. Except tests, where I create data this way: const

Rewriting state in Redux

Deadly 提交于 2019-12-06 01:07:35
In redux I understand that state is immutable and when you create new state you are essentially updating the object with what ever new information there is and then totally rewriting the state. Today I had a thought and I am not sure how stupid it is. Is it computationally expensive to keep re-writing the state? I know that this is one of the major paradigms of Redux, but I want to know if this makes sense from a memory and space perspective. You are allowed to mutate the state in Redux but you should not do it at any cost because you'd be coding in Redux anti-patterns Mutating objects, in

Append value to List

陌路散爱 提交于 2019-12-05 20:22:52
问题 I have a immutable list object, within a Map object, as follows: let initialState = Immutable.fromJS({}); state = initialState; state = state.set("myList", Immutable.List()); How do I append a value to "myList", thereby updating state ? 回答1: You can use update() method. const initialState = Immutable.fromJS({}); const state = initialState.set('myList', Immutable.List()); const updatedState = state.update('myList', myList => myList.push('some value')); 回答2: emails = List(new Array<string>());

Redux not updating components when deep Immutable state properties are updated

為{幸葍}努か 提交于 2019-12-05 05:26:54
MY QUESTION: Why doesn't updating a property of an object in an array in my Immutable state (Map) not cause Redux to update my component? I'm trying to create a widget that uploads files to my server, and my initial state (from inside my UploaderReducer which you will see below) object looks like this: let initState = Map({ files: List(), displayMode: 'grid', currentRequests: List() }); I have a thunk method that starts uploads and dispatches actions when an event occurs (such as a progress update). For example, the onProgress event looks like this: onProgress: (data) => { dispatch

How to use Immutable JS with typed ES6 classes?

假如想象 提交于 2019-12-04 23:20:28
Say I have classes Task and TaskGroup class Task{ constructor(public text:string){} } class TaskGroup { constructor(public title:string = "new task group", public tasks:Task[] = []){} } Then in my Angular 2 service I will create an Immutable List of TaskGroups @Injectable() class TaskService { taskGroups:Immutable.List<TaskGroup>; constructor() { this.taskGroups = Immutable.List<TaskGroup>([new TaskGroup("Coding tasks")]); } } This way only taskGroups List is immutable. Whatever is inside it isn't. Even if I do Immutable.fromJS(...) instead of Immutable.List<Board>(...) the nested objects are

Immutable.js Push into array in nested object

痞子三分冷 提交于 2019-12-04 22:40:26
Assume there is an object: const object = { 'foo': { 'bar': [1, 2, 3] } } I need to push 4 to object.foo.bar array. Right now I'm doing it like this: const initialState = Immutable.fromJS(object) const newState = initialState.setIn( ['foo', 'bar', object.foo.bar.length], 4 ) console.log(newState.toJS()) But I don't really like it, since I need to use object.foo.bar.length in the path. In my real example object is nested much deeper, and getting array's length looks very ugly. Is there another, more convenient way? zerkms This should work initialState.updateIn(['foo', 'bar'], arr => arr.push(4)

Redux state is undefined in mapStateToProps

爱⌒轻易说出口 提交于 2019-12-04 15:35:10
问题 I am currently following this tutorial. I've hit a bit of a snag involving mapStateToProps in the following code: import React from 'react'; import Voting from './voting'; import {connect} from 'react-redux'; const mapStateToProps = (state) => { return { pair: state.getIn(['vote','pair']), winner: state.get('winner') }; } const VotingContainer = connect(mapStateToProps)(Voting); export default VotingContainer; Here is the Voting component that's imported: import React from 'react'; import

TypeScript | Immutable | proper way of extending Immutable.Map type

半城伤御伤魂 提交于 2019-12-04 12:20:37
I have a react-redux application written in typescript with immutable package. There I have a data, which comes from api and in store I pack it to Map. In all application they are used as a Map. I created an interface: export interface PaymentMethod extends Immutable.Map<string, string | NamedType<number>> { id: string; name: string; description: string; accountNr: string; paymentMethodType: NamedType<number>; } In general it works very good. Except tests, where I create data this way: const dummyPaymentMethod: PaymentMethod = Map({ id: '', name: '', description: '', accountNr: '',

Parsing nested Records in Immutable.js

家住魔仙堡 提交于 2019-12-04 08:06:55
问题 Suppose I have the following Records defined using Immutable.js: var Address = Immutable.Record({street: '', city: '', zip: ''}); var User = Immutable.Record({name: '', address: new Address()}); How do I convert plain javascript object into the User record? I tried the following but it does not produce the expected output: var user = new User({name: 'Foo', address: {street: 'Bar', city: 'Baz'}}); // => Record { "name": "Foo", "address": [object Object] } I am aware that it is possible to

Immutable.js relationships

半世苍凉 提交于 2019-12-04 07:46:06
问题 Imagine a situation that John have two childrens Alice and Bob, and Bob have a cat Orion. var Immutable = require('immutable'); var parent = Immutable.Map({name: 'John'}); var childrens = Immutable.List([ Immutable.Map({name: 'Alice', parent: parent}), Immutable.Map({name: 'Bob', parent: parent}) ]); var cat = Immutable.Map({name: 'Orion', owner: childrens.get(1)}); After few years John wants to rename to Jane. var renamedParent = parent.set('name', 'Jane'); ...and let childrens know about it