Last active
September 15, 2016 08:39
-
-
Save pedroparra/e7d1d99ea4177b683e05 to your computer and use it in GitHub Desktop.
React Js componentDidMount
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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