bugfix> reactjs > 投稿

反応ハンドラコンポーネントに小道具を渡すにはどうすればよいですか?

私のコードは次のとおりです:

var Dashboard = require('./Dashboard');
var Comments = require('./Comments');
var Index = React.createClass({

回答 1 件
  • var Index = React.createClass({
        getInitialState: function () {
            return { input1: '', input2: '' };
        },
        handleinput1Change: function (e) {
            this.setState({ input1: e.target.value });
        },
        handleinput2Change : function (e) {
            this.setState({ input2: e.target.value });
        },
        handleSubmit: function (e) {
            e.preventDefault();
            var text1 = this.state.input1.trim();
            var text2 = this.state.input2.trim();
            if (!text1 || !text2) {
                return;
            }
            this.props.onCommentSubmit({ input1: text1, input2: text2 });
            this.setState({ input1: '', input2: '' });
        },
        render: function () {
            return (
                <form className="commentForm" onSubmit={this.handleSubmit}>
                    <input
                        type="text"
                        placeholder="Enter something"
                        value={this.state.input1}
                        onChange={this.handleinput1Change}
                    />
                    <input
                        type="text"
                        placeholder="Enter something"
                        value={this.state.input2}
                        onChange={this.handleinput2Change} />
                    <input type="submit" value="Post" />
                </form>
            );
        }
    });
    
    

あなたの答え