如何在 react js - JSX 中动态创建输入文本字段?

IT技术 javascript reactjs react-jsx
2021-05-11 21:15:25

我对 reactjs 很陌生,我遇到了一个场景,我必须创建 6 个彼此非常相似的输入字段。现在我在类的渲染方法中有这样的东西,

render () {
    return (
        <div>
            <p>
                <label htmlFor="answer1">Answer:</label><br/>
                <input
                type="text"
                name="answer1"
                id="answer1"
                className="answer"
                value={this.state.answer1}
                onChange={this._handleChange}
                />
            </p>
            <p>
                <input
                type="text"
                name="answer2"
                id="answer2"
                className="answer"
                value={this.state.answer2}
                onChange={this._handleChange}
                />
            </p>
            <p>
                <input
                type="text"
                name="answer3"
                id="answer3"
                className="answer"
                value={this.state.answer3}
                onChange={this._handleChange}
                />
            </p>
            <p>
                <input
                type="text"
                name="answer4"
                id="answer4"
                className="answer"
                value={this.state.answer4}
                onChange={this._handleChange}
                />
            </p>
            <p>
                <input
                type="text"
                name="answer5"
                id="answer5"
                className="answer"
                value={this.state.answer5}
                onChange={this._handleChange}
                />
            </p>
            <p>
                <input
                type="text"
                name="answer6"
                id="answer6"
                className="answer"
                value={this.state.answer6}
                onChange={this._handleChange}
                />
            </p>
        </div>
    );
}

代码是丑陋和多余的,无论如何我可以动态地做到这一点吗?

1个回答

如何定义这样的Answer组件(仅显示渲染方法):

render() {
  return (
    <p>
        <label htmlFor={this.props.name}>Answer:</label>
        <input
          type="text"
          name={this.props.name}
          className="answer"
          value={this.props.value}
          onChange={this.props.handleChange}
        />
    </p>  
  );
}

然后在您的父组件上,您只需导入它并像这样使用它:

var Answer = require('./answer.js');
//..

render() {
    return (
        <div>
          <Answer name="answer1" value={this.state.answer1} handleChange={this._handleChange} />
          <Answer name="answer2" value={this.state.answer2} handleChange={this._handleChange} />
          <Answer name="answer3" value={this.state.answer3} handleChange={this._handleChange} />
          // add all your Answer components
        </div>                    
    );
}

按照 Thylossus 的建议,这是一个使用示例map

var Answer = require('./answer.js');
//...

render() {
  // this is supposing you've got an answers array of { name: ..., value: ...} object
  var answers = this.state.answers.map(function(a) {
    return(<Answer name={a.name} value={a.value} handleChange={this._handleChange} />)
  });
  return (
    <div>
      { answers }
    </div>                    
  );
}