React-rails - Problems Getting Started
I'm trying to figure out how to use react-rails with my Rails 5 gem. At the moment, I'm trying to follow the tutorial in this post. https://medium.com/technically-speaking/isomorph
Solution 1:
Couple of things to fix:
varAppRolesList = React.createClass({
getInitialState: function() {
return { posts: this.props.initialAppRoles };
},
render: function() {
var posts = this.state.app_roles.map(function(app_role) {
return<AppRolekey={app_role.id}app_role={app_role} />;
});
return (
<divclassName="label">
{app_roles}
</div>
);
}
});
You are copying the initialAppRoles
prop into the posts
state variable (which is mostly an anti-pattern), but then you expect this.state.app_roles
which is never set.
You should just delete the whole getInitialState
function here and directly use:
var posts = this.props.initialAppRoles.map(function(app_role) {
return<AppRolekey={app_role.id}app_role={app_role} />;
});
Second bug to fix: you are creating an array of posts
in the render
function, but then you have app_roles
in your return which is not set. Correct that to have:
return (
<divclassName="label">
{posts}
</div>
);
I copied pasted your code into my Rails setup (which uses react-rails) and it worked with prerendering on and off.
On a side note, I strongly recommend having one component per .js.jsx
file with the underscore cased name of the component as its name. It'll make things easier to find in the future.
Post a Comment for "React-rails - Problems Getting Started"