Created
July 7, 2018 23:41
-
-
Save sejas/4c8366a3112de61afa792a636b75da6d to your computer and use it in GitHub Desktop.
Introducción a React - 06 setState para cambiar el valor del estado
This file contains 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
'use strict' | |
// 0.- Nuestro primer componente | |
class Hola extends React.Component { | |
// 5.- Añadimos la propiedad state | |
state = { | |
emoticono: '😎' | |
} | |
// 6.- Método donde utilizamos la funcion `setState` | |
cambiarEmoticono() { | |
let emoticono = '🤩' | |
if (emoticono === this.state.emoticono){ | |
emoticono = '😎' | |
} | |
this.setState({emoticono}) | |
} | |
render() { | |
// 2.- mostrar la propiedad `quien` | |
return <h1>¡Hola {this.props.quien} {this.state.emoticono}!</h1>; | |
} | |
} | |
// 4.- Definimos un componente que renderiza otro componente | |
class App extends React.Component { | |
render() { | |
return <Hola quien="Universo React" />; | |
} | |
} | |
// 1.- Renderizar nuestra App | |
ReactDOM.render( | |
// 3.- Pasar un mensaje en la propiedad `quien` | |
<App />, | |
document.getElementById("root") | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment