Skip to content

Instantly share code, notes, and snippets.

@sejas
Created July 8, 2018 00:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sejas/9d98d32331eeb4352568e97b741dffa1 to your computer and use it in GitHub Desktop.
Save sejas/9d98d32331eeb4352568e97b741dffa1 to your computer and use it in GitHub Desktop.
Introducción a React - 07 onClick interacciones con el usuario
'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