首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过react-modal提交表单时,无法读取未定义的属性“”setState“”

通过react-modal提交表单时,无法读取未定义的属性“”setState“”
EN

Stack Overflow用户
提问于 2017-02-02 05:22:25
回答 1查看 356关注 0票数 0

我觉得我错过了一些东西,可能很愚蠢。我有一个模式表单弹出,如果用户想要改变他们的头像照片。而函数正在运行并更新我的数据库,当我改变状态时,它就会变得一团糟…下面是处理模式提交的函数:

代码语言:javascript
复制
  onModalSubmit: function () {
    let data = new FormData();

    data.append('id', this.state.user._id)
    data.append('image', this.refs.image.files[0])

    axios.patch('/api/userphoto', data, {
      'Content-Type': 'multipart/form-data'
    }).then(function (response) {

      console.log(response);

      window.localStorage.setItem('user', JSON.stringify(response.data.user));

      this.setState({ user: response.data.user, modalIsOpen: false });

    }).catch(function (error) {

      console.log(error);

    })

  }

下面是模型:

代码语言:javascript
复制
  <Modal
    isOpen={this.state.modalIsOpen}
    onAfterOpen={this.afterOpenModal}
    onRequestClose={this.closeModal}
    style={customStyles}
    contentLabel="Change Photo"
  >

    <h2>Change Photo</h2>
    <div className="field">
      <div className="ui center icon input">
        <input type="file" name="image" ref="image"/>
        <button className="ui button" onClick={this.closeModal}>Cancel</button>
        <button className="ui button" onClick={this.onModalSubmit}>Submit</button>
      </div>
    </div>

  </Modal>

我已经尝试过内联绑定这个函数,也尝试过在构造函数中绑定...但它只是告诉我另一个错误,它已经绑定到组件。

同样,该函数正在工作,只是在收到服务器的响应后挂起了this.setState。

感谢您的帮助,谢谢!

EN

回答 1

Stack Overflow用户

发布于 2017-02-02 06:25:09

我试过内联绑定这个函数,也试过在构造函数中绑定...但它只是告诉我另一个错误,它已经绑定到组件。

您正在使用React.CreateClass工厂的语法。我从你之前的帖子中知道了这一点。

在这里你不需要绑定方法。它们已经被绑定了。

请查看here提供的完整答案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41989991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档