Skip to content

Instantly share code, notes, and snippets.

@sejas
Created July 7, 2018 23:41
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/4c8366a3112de61afa792a636b75da6d to your computer and use it in GitHub Desktop.
Save sejas/4c8366a3112de61afa792a636b75da6d to your computer and use it in GitHub Desktop.
Introducción a React - 06 setState para cambiar el valor del estado
'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