在映射对象时保持事件

时间:2017-09-17 19:27:03

标签: reactjs

下面我循环一个包含一堆问题的对象。渲染对象时,使用问题和输入标记,on change change事件处理程序会中断。想法?

    convertPropsIntoObject(){

    var obj = this.state.questionMap
    var data = _.map(obj, function(value, prop) {
        return (
             <div className="field" key={uuidv4()}>
                <label className="label">{prop}
                </label>
              <div className="control">
                  <textarea 
                      className="textarea" 
                      placeholder={'Enter your response here'}  
                      defaultValue={value}
                      onChange={e => this.handleChange}
                      />
              </div>
            </div>

            )

    });

    return (
        <form onSubmit={e => this.handleSubmit(e)}>
            {data}
        <button className="button is-success is-outlined">Resubmit your responses</button>
        </form>

        )



}

这是我的错误

   TypeError: Cannot read property 'handleChange' of undefined
onChange
src/Components/CompletedSubmission/CompletedIceBreakerForm.js:40
  37 |            className="textarea" 
  38 |            placeholder={'Enter your response here'}  
  39 |            defaultValue={value}
> 40 |            onChange={e => this.handleChange()}
  41 |            />
  42 |    </div>
  43 | </div>

1 个答案:

答案 0 :(得分:2)

gradle build -Pclient=client1的回调是在不同的执行上下文中调用的,因此_.map关键字将引用lodash。

使用箭头函数代替隐式this绑定:

this