現時点では、次のように .erb ファイル内でコンポーネントをレンダリングします。
<%= react_component('Counter', number: @counter) %>
このコンポーネントには、次を使用する機能がありますpromised-xhr
。
push: function(operation) {
if(operation) {
new_number = this.state.number +1;
} else {
new_number = this.state.number -1;
}
// This call works.
// this.setState({number: new_number})
XHR.post("/update", {
data: {
count: new_number
}
}).then(function(response) {
// XHR.post is successful, so I can log the response.
console.log(response.body.number);
// But setState does not work because of 'this'
this.setState({number: reponse.body.number})
})
}
this.setState({number: response.body.number)}
this
はもはやコンポーネントではないため、機能しません。React.findDOMNode(this.refs.myComponent)
コンポーネントを見つけて新しい状態をトリガーするために使用する予定です。
ただし、 を使用しreact_component
て ref を Counter コンポーネントに割り当てる方法がわかりません。