immutable.js

How to set a property as an array i initial state?

a 夏天 提交于 2019-12-23 05:29:22
问题 In my reducer I set initial state by: const initialState = fromJS({ results: [], }); However if I try to print results by initialState.get('results') I get an immutable Map . On the other hand if in my reducer (listening to an action) I set the array via ... case LOAD_SUCCESS: return state .set('results', []); ... the array will be an actual (non-Immutable) array after executing: state.get('results') (e.g. in a selector defined via reselect ) Why? 回答1: From the fromJS docs: Deeply converts

How do i sort (swap) items inside an Immutable Map?

丶灬走出姿态 提交于 2019-12-23 05:15:37
问题 I want to swap items inside an immutable list within a Map , example: const Map = Immutable.fromJS({ name:'lolo', ids:[3,4,5] }); i am trying to use splice to do the swapping, also tried with insert() an Immutable method. Lets say i want to swap from [3, 4, 5] to [3, 5, 4] , i am truing something like this: list.set('ids', list.get('ids').splice(2, 0, list.get('ids').splice(1, 1)[0]) What's the best way to sort elements inside an Immutable data structures with Immutable.js ? 回答1: You should

How to do server side rendering with Redux + Immutable js?

我是研究僧i 提交于 2019-12-23 05:10:51
问题 I would like to Use Immutable js with my App .But It's Kinda difficult for server side rendering It throws me a error like this TypeError: state.allPosts.toJS is not a function Home.js import React,{Component} from 'react'; import {Helmet} from 'react-helmet'; import {connect} from 'react-redux'; import * as allPosts from '../Redux/Actions/AllPosts_action' class Home extends Component{ renderAllPosts(){ const {posts} = this.props; return( posts.postArr.map(({id,title,body}) => { return <li

Rewriting state in Redux

牧云@^-^@ 提交于 2019-12-22 11:10:19
问题 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. 回答1: You are allowed to mutate the state in Redux

How to use Immutable JS with typed ES6 classes?

百般思念 提交于 2019-12-22 01:43:33
问题 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

Why does Immutable.js throw Invalid key path on Map.setIn()

馋奶兔 提交于 2019-12-22 01:28:41
问题 I must be missing something here because the Docs make it out as if the below code should work just fine but I get an invalid keypath error... Check this codepen. var map1 = Immutable.Map({ 'selector': { 'type': 'bar' }}); var map2 = map1.setIn(['selector', 'type'], 'foo'); console.log(map2.toJS()); 回答1: This happens because the key 'selector' has a non-Map value. setIn will work if we make sure that the value for 'selector' is also an Immutable Map: var map1 = Immutable.Map({ 'selector':

what does getIn() do in Immutable.js?

余生颓废 提交于 2019-12-21 07:12:13
问题 Unfortunately the documentation is very sparse : https://facebook.github.io/immutable-js/docs/#/Map/getIn Does anyone have an example? I am guessing that if I have a myObject like so : a: { b:{ c:"banana" } } that myObject.getIn(["a", "b", "c"]) will return banana. However the immutable objects can also be map objects which leaves me thoroughly confused. 回答1: Shortly : map.getIn(["a", "b", "c"]) is a shortcut to map.get("a").get("b").get("c") In details: You have probably got into one of the

mapStateToProps must return an object. Instead received Map {}?

。_饼干妹妹 提交于 2019-12-20 14:24:38
问题 Hello i use Immuteble Map for state and when i try maspStateToProps i have this error. Uncaught Invariant Violation: mapStateToProps must return an object. Instead received Map {}. Here is my code: Component: const mapStateToProps = (state) => { return state } class LoanCalculator extends React.Component{ componentWillMount(){ this.dispatch(loadConstraints()); } render(){ return ( <div> <h1> Loan Calculator </h1> <SlidersBox {...this.props}/> </div> ) } } LoanCalculator = connect(

mapStateToProps must return an object. Instead received Map {}?

寵の児 提交于 2019-12-20 14:22:35
问题 Hello i use Immuteble Map for state and when i try maspStateToProps i have this error. Uncaught Invariant Violation: mapStateToProps must return an object. Instead received Map {}. Here is my code: Component: const mapStateToProps = (state) => { return state } class LoanCalculator extends React.Component{ componentWillMount(){ this.dispatch(loadConstraints()); } render(){ return ( <div> <h1> Loan Calculator </h1> <SlidersBox {...this.props}/> </div> ) } } LoanCalculator = connect(

How to construct subclasses of Immutable.Record?

拟墨画扇 提交于 2019-12-19 16:59:23
问题 class Event extends Immutable.Record { constructor(text) { super({text: text, timestamp: Date.now()}); } } Calling new Event() seems to return a constuctor function: new Event('started').toString() "function Record(values){ if(values instanceof RecordType){ return values;} if(!(this instanceof RecordType)){ return new RecordType(values);} if(!hasInitialized){ hasInitialized=true; var keys=Object.keys(defaultValues); setProps(RecordTypePrototype,keys); RecordTypePrototype.size=keys.length;