gengns ES

An entry from the gengns log, written by Génesis García Morilla · · Tooling

Basic React example on JSFiddle

Basic React example to synchronize (write) text in an HTML heading tag while typing in an input tag.

Babel + JSX

const Input = React.createClass({
  getInitialState: function () {
    return {
      userInput: ''
    };
  },
  
  handleUserInput: function (e) {
    this.setState({
      userInput: e.target.value
    });
  },
  
  render: function () {
    return (
      <div>
        <input value={this.state.userInput} 
          onChange={this.handleUserInput} type="text" />
        <h1>{this.state.userInput}</h1>
      </div>
    );
  }
});

ReactDOM.render(
  <Input />,
  document.getElementById('app')
);

HTML

<div id="app"></div>

CSS

input {
  font-size: 6vh;
}
Open in JSFiddle

Do not forget to set Babel on JSFiddle and add react.js and react-dom.js libraries.

Tags: input, React, synchronization