React and Multiple form fields

前端 未结 3 1348
星月不相逢
星月不相逢 2020-12-25 15:35

I was reading documentation on \"onChange\" and I am curious as to what I would do if my forum has multiple fields like select boxes, checkboxes, text areas and inputs? Do I

3条回答
  •  無奈伤痛
    2020-12-25 16:02

    @FakeRainBrigand 's answer is pretty cool.

    I want to share one in JavaScript style (use high-order function), much shorter:

    /** @jsx React.DOM */
    
    var App = React.createClass({
      getInitialState: function () {
        return {
          username: '',
          password: ''
        }
      },
      handleChange: function (key) {
        return function (e) {
          var state = {};
          state[key] = e.target.value;
          this.setState(state);
        }.bind(this);
      },
      render: function(){
        console.log(JSON.stringify(this.getFormData(), null, 4));
        return (
          
    Username:
    Password:
    {JSON.stringify(this.getFormData(), null, 4)}
    ); } }); React.renderComponent(, document.body);

提交回复
热议问题