class RangeIterator {
constructor(start, stop) {
this.value = start;
this.stop = stop;
}
[Symbol.iterator]() { return this; }
const convertQueryParamsToProps = query => | |
query.split("&") | |
.map(item => item.split("=")) | |
.reduce((params, queryParam) => | |
(params[queryParam[0]]= queryParam[1]) && params | |
, {}) | |
} | |
convertQueryParamsToProps("tomas=mola&foo=1") | |
//{tomas: "mola", foo: "1"} |
const to = promise =>{ | |
return promise.then(data =>{ | |
return [null, data] | |
}) | |
.catch(err => [err]) | |
} | |
fetchData(){ | |
let err, data | |
[err, data] = await to(Api.getEntity()) |
Function.prototype.pensar = function(){console.log("Soy la function " + this.name + " y estoy pensando")} | |
function ejemplo_de_f unction() { console.log('no hago nada') } | |
ejemplo_de_function.pensar() | |
// >Soy la function ejemplo_de_function y estoy pensando |
describe('Test the states', () => { | |
it('should start with operations empty list', () => { | |
const component = shallow(<App />); | |
const child = shallow(component.get(0)); | |
expect(child.state('operations')).to.be.eql([]); | |
}) | |
}); |
The 0.13.0
improvements to React Components are often framed as "es6 classes" but being able to use the new class syntax isn't really the big change. The main thing of note in 0.13
is that React Components are no longer special objects that need to be created using a specific method (createClass()
). One of the benefits of this change is that you can use the es6 class syntax, but also tons of other patterns work as well!
Below are a few examples creating React components that all work as expected using a bunch of JS object creation patterns (https://github.com/getify/You-Dont-Know-JS/blob/master/this%20&%20object%20prototypes/ch4.md#mixins). All of the examples are of stateful components, and so need to delegate to React.Component
for setState()
, but if you have stateless components each patterns tends to get even simpler. The one major caveat with react components is that you need to assign props
and context
to the component instance otherwise the component will be static. The reason is
callLog = []; | |
/* set up an override for the Function call prototype | |
* @param func the new function wrapper | |
*/ | |
function registerOverride(func) { | |
oldCall = Function.prototype.call; | |
Function.prototype.call = func; | |
} |
Monitor events by ID:
monitorEvents(document.getElementById('id'))
Monitor events by class:
monitorEvents(document.querySelector('.class'))
//http://danburzo.ro/grunt/chapters/handlebars/ | |
//In templating languages, partials are templates that can be reused in other templates. In Handlebars, you use the {{> partial }} helper to include partials. Let's take an example: | |
//Note: It's important to register the partial before compiling any template that includes it, otherwise it will throw an error. | |
<script type='text/x-handlebars' id='post-list-template'> | |
<h2>{{ title }}</h2> | |
<ul> | |
{{#each posts}} | |
<li>{{> post-item}}</li> | |
{{/each}} |
const getEditorState = this.props.store.getItem('getEditorState'); | |
const setEditorState = this.props.store.getItem('setEditorState'); | |
const selection = this.props.store.getItem('lastSelection'); | |
const editorState = getEditorState(); | |
const updateSelection = new SelectionState({ | |
anchorKey: selection.anchorKey, | |
anchorOffset: selection.anchorOffset, | |
focusKey: selection.anchorKey, | |
focusOffset: selection.focusOffset, | |
isBackward: false, |