JSON stringify a Set

JSON.stringify doesn’t directly work with sets because the data stored in the set is not stored as properties. But you can convert the set to an array. Then you will be able to stringify it properly. Any of the following will do the trick: JSON.stringify([…s]); JSON.stringify([…s.keys()]); JSON.stringify([…s.values()]); JSON.stringify(Array.from(s)); JSON.stringify(Array.from(s.keys())); JSON.stringify(Array.from(s.values()));

Is it possible to sort a ES6 map object?

According MDN documentation: A Map object iterates its elements in insertion order. You could do it this way: var map = new Map(); map.set(‘2-1’, “foo”); map.set(‘0-1’, “bar”); map.set(‘3-1′, “baz”); var mapAsc = new Map([…map.entries()].sort()); console.log(mapAsc) Using .sort(), remember that the array is sorted according to each character’s Unicode code point value, according to the string … Read more

Filter or map nodelists in ES6

[…nodelist] will make an array of out of an object if the object is iterable. Array.from(nodelist) will make an array out of an object if the object is iterable or if the object is array-like (has .length and numeric props) Your two examples will be identical if NodeList.prototype[Symbol.iterator] exists, because both cases cover iterables. If … Read more

setState() inside of componentDidUpdate()

You can use setStateinside componentDidUpdate. The problem is that somehow you are creating an infinite loop because there’s no break condition. Based on the fact that you need values that are provided by the browser once the component is rendered, I think your approach about using componentDidUpdate is correct, it just needs better handling of … Read more

How to set component default props on React component

You forgot to close the Class bracket. class AddAddressComponent extends React.Component { render() { let {provinceList,cityList} = this.props if(cityList === undefined || provinceList === undefined){ console.log(‘undefined props’) } else { console.log(‘defined props’) } return ( <div>rendered</div> ) } } AddAddressComponent.contextTypes = { router: React.PropTypes.object.isRequired }; AddAddressComponent.defaultProps = { cityList: [], provinceList: [], }; AddAddressComponent.propTypes = … Read more

setState doesn’t update the state immediately [duplicate]

Your state needs some time to mutate, and since console.log(this.state.boardAddModalShow) executes before the state mutates, you get the previous value as output. So you need to write the console in the callback to the setState function openAddBoardModal() { this.setState({ boardAddModalShow: true }, function () { console.log(this.state.boardAddModalShow); }); } setState is asynchronous. It means you can’t … Read more