Skip to content

Instantly share code, notes, and snippets.

@tomcask
tomcask / 0. intro.md
Created March 15, 2017 07:59 — forked from jquense/0. intro.md
Alternative ways to define react Components

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

@tomcask
tomcask / TestStateinHOC.js
Created March 22, 2017 14:50
How you test State in High order components
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([]);
})
});

Como trabajan los iteradores

class RangeIterator {
  constructor(start, stop) {
    this.value = start;
    this.stop = stop;
  }
 
 [Symbol.iterator]() { return this; }
@tomcask
tomcask / Function_exits_in_all_functions.js
Last active May 26, 2019 02:02
Objectos en javascript
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
@tomcask
tomcask / await-manage-cacth-without-try.js
Created February 23, 2018 07:59
try to avoid use try catch statement and manage the error in await call
const to = promise =>{
return promise.then(data =>{
return [null, data]
})
.catch(err => [err])
}
fetchData(){
let err, data
[err, data] = await to(Api.getEntity())
@tomcask
tomcask / convertQueryParamsToProps.js
Created November 16, 2018 08:21
returns an object reliable like as props of React components
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"}