Javascript 在react组件中使用ajax获取实时更新的数据

Javascript 在react组件中使用ajax获取实时更新的数据,javascript,ruby-on-rails,ajax,ruby-on-rails-4,reactjs,Javascript,Ruby On Rails,Ajax,Ruby On Rails 4,Reactjs,故事: ... _add(){ $.ajax({ url: ''url-to-the-controller-action-(add), type: 'POST', success: function (data) { console.log("DB is set to: " this.props.uid) }, error: function (data) { console.log("not added")

故事:

...

_add(){
  $.ajax({
    url: ''url-to-the-controller-action-(add),
    type: 'POST',
    success: function (data) {
        console.log("DB is set to: " this.props.uid)
    },
    error: function (data) {
        console.log("not added")
    }
  });
},

_remove(){
  $.ajax({
    url: ''url-to-the-controller-action-(remove),
    type: 'POST',
    success: function (data) {
        console.log("DB is set to: " this.props.uid) 
    },
    error: function (data) {
        console.log("not removed")
    }
  });
},

render(){
  return(
    <div>
      <button onClick={this._add}>add</button>
      <button onClick={this._remove}>remove</button>
    </div>
  )
}

...
<Button uid={this.state.uid} />
用户单击“添加”,它将数据库中的值设置为
true
。单击“删除”,将数据库中的同一列更新为
false

def add
  u = User.find(1)
  u.foo = true
  u.save
  u.reload
  head :ok
end

def remove
  u = User.find(1)
  u.foo = false
  u.save
  u.reload
  head :ok
end
反应按钮组件:

...

_add(){
  $.ajax({
    url: ''url-to-the-controller-action-(add),
    type: 'POST',
    success: function (data) {
        console.log("DB is set to: " this.props.uid)
    },
    error: function (data) {
        console.log("not added")
    }
  });
},

_remove(){
  $.ajax({
    url: ''url-to-the-controller-action-(remove),
    type: 'POST',
    success: function (data) {
        console.log("DB is set to: " this.props.uid) 
    },
    error: function (data) {
        console.log("not removed")
    }
  });
},

render(){
  return(
    <div>
      <button onClick={this._add}>add</button>
      <button onClick={this._remove}>remove</button>
    </div>
  )
}

...
<Button uid={this.state.uid} />
Foo的渲染:

...

_add(){
  $.ajax({
    url: ''url-to-the-controller-action-(add),
    type: 'POST',
    success: function (data) {
        console.log("DB is set to: " this.props.uid)
    },
    error: function (data) {
        console.log("not added")
    }
  });
},

_remove(){
  $.ajax({
    url: ''url-to-the-controller-action-(remove),
    type: 'POST',
    success: function (data) {
        console.log("DB is set to: " this.props.uid) 
    },
    error: function (data) {
        console.log("not removed")
    }
  });
},

render(){
  return(
    <div>
      <button onClick={this._add}>add</button>
      <button onClick={this._remove}>remove</button>
    </div>
  )
}

...
<Button uid={this.state.uid} />

我相信我可以实现这一点,而不需要通量/重复使用,因为这将是对这个微小组件的过度杀戮。但不知道如何使用它

该应用程序可以工作,但我必须刷新页面才能看到更改


或者,Rails 5是我使用action cable的最佳选择?

成功后,您需要通知Foo组件,因为uid已更改

Foo组件中:

onChange: (uid) ->
   this.setState({uid: uid})

render: ->
  <Button uid={this.state.uid} onChange={this.onChange}/>
_add(){
  $.ajax({
    url: ''url-to-the-controller-action-(add),
    type: 'POST',
    success: function (data) {
     this.props.onChange(data["uid"])
     console.log("DB is set to:", data["uid"])
    },
    error: function (data) {
       console.log("not added")
    }
  });
},
在你的行动中:

def add
  ...
  render json: { uid: u.foo }
end
关于为什么需要在此刷新:

当您进行ajax调用时,数据库中的数据会更新。但是在成功回调时,您需要使用新数据更新视图。当您记录this.props.uid时,您将旧数据记录在内存中,当您点击refresh时,数据将被数据库中的新数据替换

例如,在jQuery中,您需要用成功回调中传递的新数据替换DOM中的元素。在ReactJS中,需要更新状态以重新渲染组件


在这里,您的组件按钮Foo的子组件,并像道具一样接收uid。因此,您需要通知父组件thanuid已更改为更新状态,并重新呈现自身及其子组件。

成功后,您需要通知Foo组件thanuid已更改

Foo组件中:

onChange: (uid) ->
   this.setState({uid: uid})

render: ->
  <Button uid={this.state.uid} onChange={this.onChange}/>
_add(){
  $.ajax({
    url: ''url-to-the-controller-action-(add),
    type: 'POST',
    success: function (data) {
     this.props.onChange(data["uid"])
     console.log("DB is set to:", data["uid"])
    },
    error: function (data) {
       console.log("not added")
    }
  });
},
在你的行动中:

def add
  ...
  render json: { uid: u.foo }
end
关于为什么需要在此刷新:

当您进行ajax调用时,数据库中的数据会更新。但是在成功回调时,您需要使用新数据更新视图。当您记录this.props.uid时,您将旧数据记录在内存中,当您点击refresh时,数据将被数据库中的新数据替换

例如,在jQuery中,您需要用成功回调中传递的新数据替换DOM中的元素。在ReactJS中,需要更新状态以重新渲染组件


在这里,您的组件按钮Foo的子组件,并像道具一样接收uid。因此,您需要通知父组件,uid已更改为更新状态,并重新渲染自身及其子组件。

确定。我今天晚些时候去。我会回来报到的。谢谢。我今天晚些时候去。我会回来报到的。谢谢