I would like to update the value with a random number generated from the controller each time the div is clicked. Since the value is coming down statically from the Rails controller, does this mean I have to submit an ajax request to the route?
I understand that I can achieve the same effect by setting the state with some JavaScript code, but I want to know particularly if I can get this value from the controller.
Please help me fill in the handleClick
function.
The react-rails gem is being used to render components in the view.
controller
class PostsController < ApplicationController
def index
@num = (1..100).to_a.sample
end
end
view
<%= react_component("Num", { value: @num }) %>
component
var Num = React.createClass({
getInitialState: function() {
return {
value: this.props.value
};
},
handleClick: function() {
},
render: function() {
return (
<div className="num" onClick={this.handleClick}>
{ this.state.value }
</div>
);
}
});