「React入門」selectドロップダウンメニューのonchangeイベントを使うサンプル

書式
handleChange(event){ some code}
handleSubmit(event) {some code}
<select value={this.state.value} onChange={this.handleChange}>
使用例

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>React サンプル</title>
<script src="/react/16.4.0/umd/react.development.js"></script>
<script src="/react-dom/16.4.0/umd/react-dom.development.js"></script>
<script src="/babel-standalone/6.26.0/babel.min.js"></script>
</head>
<body>
<div id="arkgame"></div>

<script type="text/babel">
class CftForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {value: 'default value'};

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

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

  handleSubmit(event) {
    alert('選択した都市: ' + this.state.value);
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          都市を選択してください
          <select value={this.state.value} onChange={this.handleChange}>
            <option value="tokyo">東京</option>
            <option value="oosaka">大阪</option>
            <option value="fukuoka">福岡</option>
            <option value="kawasaki">川崎</option>
          </select>
        </label>
        <input type="submit" value="選択" />
      </form>
    );
  }
}

ReactDOM.render(
  <CftForm />,
  document.getElementById('arkgame')
);
</script>

</body>
</html>

 

React.js

Posted by arkgame