reactjs - Flux, how to correctly reuse components? -
still studying flux, i'm in trouble logic store. let's i've created component, single button manage vote, switch "yes"/"no".
so have votebutton manage voteaction use dispatcher call store function change votedbyviewer state.
all it's fun , it's right... if have single button. if have multiples, share same store , re-render happen on multiples components.
this initial js:
// parse div react management $("#be-content [id*=like]").each(function (index) { var div_id = $(this).attr("id"); // render votebutton reactdom.render( <votebutton />, document.getelementbyid(div_id) ) });
each votebutton correctly rendered in page each switch change state of buttons. votebutton:
var votestore = require('../stores/votestore') ; var voteactions = require('../actions/voteactions'); var votebutton = react.createclass({ getinitialstate: function () { return { voted: votestore.getvote() } }, componentdidmount: function () { votestore.addvotelistener(this._onchange); }, componentwillunmount: function () { votestore.removevotelistener(this._onchange); }, handlevote: function () { this.state.voted ? voteactions.unvote() : voteactions.vote() }, _onchange: function () { this.setstate({ voted: votestore.getvote() }) }, render: function () { return ( <div classname="link"> <button onclick={this.handlevote}>{this.state.voted? 'yes':'no'}</button> </div> ) } }); module.exports = votebutton;
any idea on how solve it? maybe have use containers? thanks
all need set 1 id each button using. when click it's passing id action, dispatcher, store receive it. store array , each key button id. when store receive data changes needed id. , button state of id ( getvote('buttonid');)
Comments
Post a Comment