私はreact-rails
gemのRailsアプリを持っています。React-Railsからオブジェクトを追加または削除するには?
これは私のコントローラである:
class WebsitesController < ApplicationController
def index
@websites = Website.all
end
end
は、これが私の見解です:
<%= react_component('WebsiteList', websites: @websites) %>
これは私のコンポーネントウィットリアクトされています。それだけで、配列をマッピングしていますWebsiteItemで
class WebsiteList extends React.Component {
render() {
return (
<div className="container">
<AddWebsite/>
<WebsiteItem websites={this.props.websites}/>
</div>
);
}
}
WebsiteList.propTypes = {
websites: React.PropTypes.node
};
をし、各オブジェクトを表示します。私もコンポーネントに反応更新したいと思い、リンクおよびAjaxは成功である
class AddWebsite extends React.Component {
constructor() {
super();
this.state = {
formValue: "http://www."
}
}
handleChange(e) {
this.setState({formValue: e.target.value});
}
submitForm() {
$.ajax({
method: "POST",
url: "/websites",
data: { website: {name: "New Web", url: this.state.formValue} }
})
.done(function() {
console.log("done")
});
}
render() {
return (
<form>
<input value={this.state.formValue} onChange={this.handleChange.bind(this)} name="url"/>
<button type="button" onClick={this.submitForm.bind(this)} className="btn">Add Web</button>
</form>
);
}
}
誰かが追加(または削除):アヤックスとの
AddWebsiteコンポーネントは、サーバー上のデータを取得します。 this.props.websites & propTypeの代わりに、私は州のためにそれをどうやって行うことができますか?私はそれをajaxから取得する必要がありますか、またはerbで<%=react_component ... %>
を使用できますか?
これを解決し、基本的に1ページのアプリケーションを作成する最も良い方法は何ですか?
私は理解していませんが、コンポーネントの状態を変更するためにコールバックでReactのsetStateを使用することはできませんか? –
私はそれをやろうとしました - 今のように私はPropTypesのウェブサイトから得た "this.props.websites"を持っています:React.PropTypes.node。私はthis.state = {ウェブサイト:...}をやろうとしましたが、3つのドットの代わりに何が必要かを理解しませんでした。 – Dudis
ああ、ちょっと答えをつかまえます。 –