(guest@joequery.me)~ $ |

Fun with Klipse - interactive client side code evaluation

Oh hi there!

Today let's try to embed interactive JS snippets on this blog using Klipse. It seems like it would be a great addition to the blog. Let's do it!

Array .map() example



[1, 2, 3].map(function(x){
    return x + 1;
});

Array .reduce() example



[1, 2, 3].reduce(function(total, x){
    return total + x
});

React Component example



class FlavorForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {value: 'coconut'};

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange(event) {
    this.setState({value: event.target.value});
  }

  handleSubmit(event) {
    alert('Your favorite flavor is: ' + this.state.value);
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Pick your favorite La Croix flavor:
          <select value={this.state.value} onChange={this.handleChange}>
            <option value="grapefruit">Grapefruit</option>
            <option value="lime">Lime</option>
            <option value="coconut">Coconut</option>
            <option value="mango">Mango</option>
          </select>
        </label>
        <input type="submit" value="Submit" />
      </form>
    );
  }
}
ReactDOM.render(React.createElement(FlavorForm, null), container);

See the output below.

Date published - April 24, 2021