Created
July 8, 2018 00:13
-
-
Save sejas/9d98d32331eeb4352568e97b741dffa1 to your computer and use it in GitHub Desktop.
Introducción a React - 07 onClick interacciones con el usuario
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` | |
// 7.- añadimos un botón con el atributo onClick | |
return <div> | |
<h1>¡Hola {this.props.quien} {this.state.emoticono}!</h1> | |
<button onClick={this.cambiarEmoticono}>Cambiar estado</button> | |
</div>; | |
} | |
} | |
// 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