George_Fal 2015-02-23 13:04 采纳率: 0%
浏览 38

Ajax和React函数

I have this ajaxify form which submits the data well, however I want to pass data from this ajax code to a React component to update the view after successfully submitting the data. How do I pass some data to React using a function?

When using the code below it gives me 'unidentified function', how can make it accessible to both ajax and React?

$('#upload').ajaxForm({
  success: function(data) {
    newlocal(data);
  }
});

And this is the React code I use:

var Postlist = React.createClass({
  getInitialState: function(){
    return {posts: []};
  },
  newLocal : function(data){
    alert(data);
  },
  • 写回答

1条回答 默认 最新

  • 七度&光 2015-02-23 15:10
    关注

    The callback that you are passing to the ajaxForm should do a setState. I assume you have your form component to which can pass props like below. Since you are accessing the actual DOM element, in this case #upload, it can be done only with componentDidMount.

    var PostList = React.createClass({
      getInitialState: function() {
        return {
          posts: []
        };
      },
    
      _handleSubmitSuccess: function(httpStatus, createdPost) {
        var that = this;
        that.setState({posts: (that.state.posts << createdPost)});
      },
    
      posts: function() {
        this.state.posts.map(function(post) {
          return (
            <div>
            ...
            </div>
          );
        });
      },
    
      render: function() {
        return (
          <div>
            {this.posts()}
    
            <br />
    
            <PostForm onSubmitSuccess={this._handleSubmitSuccess} />
          </div>
        );
      }
    });
    
    
    var PostForm = React.createClass({
      componentDidMount: function() {
        var that = this;
    
        $('#upload').ajaxForm({
          success: that.props.onSubmitSuccess
        });
      },
    
      render: function() {
        return (
          <form id="#upload">
          ...
          </form>
        );
      }
    });
    

    Hope you get the idea.

    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制