I have a Rails app with react-rails
gem.
This is my controller:
class WebsitesController < ApplicationController
def index
@websites = Website.all
end
end
This is my view:
<%= react_component('WebsiteList', websites: @websites) %>
This is my React component wit:
class WebsiteList extends React.Component {
render () {
return (
<div className="container">
<AddWebsite/>
<WebsiteItem websites={this.props.websites}/>
</div>
);
}
}
WebsiteList.propTypes = {
websites: React.PropTypes.node
};
In WebsiteItem it's just mapping the array and showing each object.
AddWebsite Component with ajax to get data on the server:
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>
);
}
}
When someone adds (or delete) a link and ajax is success, I'd like to update the React component as well. Instead of this.props.websites & propTypes - how I could do it for state? Do I need to get the it from ajax or can I use the <%=react_component ... %>
in erb?
What is the best way to solve it and create basically a single page app?