-
-
Save jmbejar/80f03eaea5bf8ec3b55935ddf64d7a90 to your computer and use it in GitHub Desktop.
Code example 1 in "Unopinionated comparison of Glimmer and React"
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
import Component, { tracked } from '@glimmer/component'; | |
const animals = ["Cat", "Dog", "Rabbit"]; | |
export default class extends Component { | |
@tracked randomAnimal = null; | |
setRandomAnimal() { | |
const animal = animals[Math.floor(Math.random() * 3)]; | |
this.randomAnimal = animal; | |
} | |
} |
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
import React, { Fragment } from "react"; | |
const animals = ["Cat", "Dog", "Rabbit"]; | |
class RandomAnimal extends React.Component { | |
constructor() { | |
super(); | |
this.state = { animal: null }; | |
this.setRandomAnimal = this.setRandomAnimal.bind(this); | |
} | |
setRandomAnimal() { | |
const animal = animals[Math.floor(Math.random() * 3)]; | |
this.setState({ animal }); | |
} | |
render() { | |
let renderAnimal; | |
if (this.state.animal) { | |
renderAnimal = <h1>Hello, {this.state.animal}</h1> | |
} | |
return ( | |
<Fragment> | |
<button onClick={this.setRandomAnimal}>Set Random Animal</button> | |
{ renderAnimal } | |
</Fragment> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment