immutable.js

Delete object from ImmutableJS List based upon property value

蹲街弑〆低调 提交于 2019-12-03 22:34:04
What would be the simplest way to delete an object from a List based on a value of a property? I'm looking for an equivalent of the $pull in MongoDB. My List looks simple like this: [{a: '1' , b: '1'},{a: '2' , b: '2'}] And I'd like to remove from the array the object with property a set to '1'. In MongoDB, I'd do it like this: Model.update({_id: getCorrectParentObj},{ $pull: {listIDeleteFrom: { a: '1' } } },(err, result)=>{}); How can I get the same result with ImmutableJS? okm You could simply filter the immutable list: var test = Immutable.List.of(Immutable.Map({a: '1'}), Immutable.Map({a:

Getting nested values in Immutable.js

做~自己de王妃 提交于 2019-12-03 14:37:39
问题 According to the docs here: https://facebook.github.io/immutable-js/docs/#/Map/getIn I should be able to get the deeply nested value by providing an array for the keyPath argument. This is what I've done, however I'm getting undefined as the return value. http://jsfiddle.net/srxv4mwu/ var obj = Immutable.Map({categories: {1: 'a', 2: 'b'}}); var output = obj.getIn(['categories', 1]); alert(output); What am I doing wrong? 回答1: A map is simply a collection of keys with values. What you have is a

Redux state is undefined in mapStateToProps

蹲街弑〆低调 提交于 2019-12-03 10:40:33
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 Vote from './Vote'; import Winner from './winner'; const Voting = ({pair,vote,hasVoted,winner}) => <div>

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

ぐ巨炮叔叔 提交于 2019-12-03 10:30:22
How can I achieve the following using ImmutableJS: myMap.get(key).push(newData); 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 the new data. So, you would have to set it to a variable in order to access it (in this case, myNewMap ). If

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

时光总嘲笑我的痴心妄想 提交于 2019-12-03 03:45:51
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 )(LoanCalculator) export default LoanCalculator REDUCER import { Map } from 'immutable' import

What is the difference between ImmutableJS Map() and fromJS()?

隐身守侯 提交于 2019-12-03 02:36:54
问题 var a = {address: {postcode: 5085}} var b = Immutable.fromJS(a) var c = b.setIn(['address', 'suburb'], 'broadview').toJS(); // no error console.log(c); var d = Immutable.Map(a); var e = d.setIn(['address', 'suburb'], 'broadview').toJS(); // error invalid keyPath(…) Could someone explain the difference. Thanks, 回答1: In this example, var a = {address: {postcode: 5085}} var d = Immutable.Map(a); Here, d.get('address') is immutable . It's value cannot change to any other objects. We can only

When to use .toJS() with Immutable.js and Flux?

一个人想着一个人 提交于 2019-12-03 02:12:48
问题 I'm trying to use ImmutableJS with my React / Flux application. My stores are Immutable.Map objects. I'm wondering at which point should I use .toJS() ? Should it be when the store's .get(id) returns ? or in the components with .get('member') ? 回答1: Ideally, never! If your Flux stores are using Immutable.js then try to maintain all the way through. Use React.addons.ReactComponentWithPureRenderMixin to achieve a memoization performance win (it adds a shouldComponentUpdate methods). When

Parsing nested Records in Immutable.js

我的梦境 提交于 2019-12-02 20:48:14
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 explicitly create the Address record: var user = new User({name: 'Foo', address: new Address({street: 'Bar'

When to use .toJS() with Immutable.js and Flux?

感情迁移 提交于 2019-12-02 17:16:07
I'm trying to use ImmutableJS with my React / Flux application. My stores are Immutable.Map objects. I'm wondering at which point should I use .toJS() ? Should it be when the store's .get(id) returns ? or in the components with .get('member') ? Lee Byron Ideally, never! If your Flux stores are using Immutable.js then try to maintain all the way through. Use React.addons.ReactComponentWithPureRenderMixin to achieve a memoization performance win (it adds a shouldComponentUpdate methods). When rendering, you may need to call toJS() as React v0.12.x only accepts Array as children: render: function

Immutable.js relationships

不羁的心 提交于 2019-12-02 16:40:56
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. childrens = childrens.map(function(children) { children.set('parent', renamedParent); }); Then I have