Skip to content

Instantly share code, notes, and snippets.

@pedroparra
Last active September 15, 2016 08:39
Show Gist options
  • Select an option

  • Save pedroparra/e7d1d99ea4177b683e05 to your computer and use it in GitHub Desktop.

Select an option

Save pedroparra/e7d1d99ea4177b683e05 to your computer and use it in GitHub Desktop.
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');
// Pedimos algunos datos
$.get(this.props.source, function(result) {
var dato = result[0];
this.setState({ data: [ dato ] });
}).bind(this));
},
render: function() {
<div>Soy un component</div>
}
});
// Ecmascript 6
class MyComponent extends React.Component {
constructor(props){
super(props);
this.state = { data: [] }
console.log('El componente aun no se ha montado');
}
componentDidMount() {}
console.log('El componente está disponible en el DOM');
// Pedimos algunos datos
$.get(this.props.source, (result) => {
let dato = result[0];
this.setState({ data: [ dato ] });
}).bind(this));
}
render(){
<div>Soy un component</div>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment