Skip to content

Instantly share code, notes, and snippets.

@jmbejar
Last active December 20, 2017 16:34
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 jmbejar/80f03eaea5bf8ec3b55935ddf64d7a90 to your computer and use it in GitHub Desktop.
Save jmbejar/80f03eaea5bf8ec3b55935ddf64d7a90 to your computer and use it in GitHub Desktop.
Code example 1 in "Unopinionated comparison of Glimmer and React"
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;
}
}
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>
);
}
}
<button onclick={{action setRandomAnimal}}>Set Random Animal</button>
{{#if randomAnimal}}
Hello, {{randomAnimal}}!
{{/if}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment