Skip to content

Instantly share code, notes, and snippets.

View pedroparra's full-sized avatar
🏠
Working from home

Pedro J. Parra pedroparra

🏠
Working from home
View GitHub Profile
@pedroparra
pedroparra / Enhance.js
Created March 8, 2016 19:25 — forked from sebmarkbage/Enhance.js
Higher-order Components
import { Component } from "React";
export var Enhance = ComposedComponent => class extends Component {
constructor() {
this.state = { data: null };
}
componentDidMount() {
this.setState({ data: 'Hello' });
}
render() {
@pedroparra
pedroparra / store-example.js
Last active February 12, 2019 07:58
Ejemplo de store con redux.
import React from 'react';
import ReactDOM from 'react-dom';
import { createStore } from 'redux';
// Preparamos nuestra funcion reducer
const myReducer = (state = 0, action) => {
switch(action.type) {
case 'sumar':
return state + 1;
case 'restar':
@pedroparra
pedroparra / reducer-example.js
Last active January 3, 2016 09:25
Ejemplo de función reductora en Redux.
// Reducer - Función Pura
// Usando es6
// Nunca cambiamos el state
// Devolvemos 0 si el state es undefined
// Devolvemos el state si no conocemos el action
const myReducer = (state = 0, action) => {
switch(action.type) {
case 'sumar':
return state + 1;
@pedroparra
pedroparra / functions.js
Created January 2, 2016 21:47
Funciones puras e impuras en javascript.
// Funcion Pura
function doble(num) {
return num * 2; // No modifica el argumento.
}
function doblarLista(listado){
// Retorna un nuevo listado sin modificar nada.
return listado.map(doble);
}
@pedroparra
pedroparra / componentDidUpdate.js
Created August 13, 2015 19:32
React Js componentDidUpdate
// El metodo recive las propiedades previas y el estado previo.
componentDidUpdate: function(prev_props, prev_state) {}
@pedroparra
pedroparra / shouldComponentUpdate.js
Last active August 29, 2015 14:27
React Js shouldComponentUpdate
// Ecmascript 5
var MyComponent = React.createClass({
shouldComponentUpdate: function(next_props, next_state) {
return false;
}
// Despues del primer render, nunca volver a renderizarse.
render: function() {
<div>Soy un component</div>
@pedroparra
pedroparra / componentWillReceiveProps.js
Created August 13, 2015 19:03
React Js componentWillReceiveProps
// Ecmascript 5
var MyComponent = React.createClass({
componentWillReceiveProps: function(next_props){
this.setState({ loading: true });
},
render: function() {
className = this.state.loading ? 'loading' : 'loaded';
return(<div className={className}>Soy un component</div>)
@pedroparra
pedroparra / componentDidMount.js
Last active September 15, 2016 08:39
React Js componentDidMount
// Ecmascript 5
var MyComponent = React.createClass({
componentWillMount: function() {
console.log('El componente aun no está disponible en el DOM');
return { data:[] };
},
componentDidMount: function() {
console.log('El componente está disponible en el DOM');
@pedroparra
pedroparra / componentWillMount.js
Last active August 29, 2015 14:27
React Js componentWillMount
// Ecmascript 5
var MyComponent = React.createClass({
componentWillMount: function() {}
console.log('El componente aun no está disponible en el DOM');
return { data:[] };
},
render: function() {
<div>Soy un component</div>
}
});
@pedroparra
pedroparra / example
Created June 24, 2015 11:14
npm list global without listing the dependencies.
npm ls --depth 0 -g