Reactjs component interfering other component on same page -
i have 2 react components, likebox
, commentbox
likes.jsx
var react = require('react'); var likebox = react.createclass({ getinitialstate: function(){ return {count: this.props.initialcount} }, render: function(){ return ( <div classname="likebox"> { this.state.count && <span>{this.state.count} people this</span> } </div> ); } }) module.exports.likebox = likebox;
comment_box.jsx
var react = require('react'); var $ = require('app/common/jquery'); var commentbox = react.createclass({ getinitialstate: function(){ return {data: []}; }, handlecommentsubmit: function(comment){ this.refreshcomments(); }, apiurl: function(){ return this.props.apiurl+"?content_uuid="+this.props.contentuuid; }, refreshcomments: function(){ $.get(this.apiurl(), function(data){ this.setstate({data: data}); }.bind(this), 'json'); }, componentdidmount: function(){ this.refreshcomments(); }, render: function(){ return ( <div classname="commentbox"> <h1>comments</h1> <commentlist data={this.state.data} /> <commentform oncommentsubmit={this.handlecommentsubmit} apiurl={this.apiurl()}/> </div> ); } }); ... module.exports.commentbox = commentbox;
and have 2 jquery plugins mount divs components:
likes.js
"use strict"; var $ = require('jquery'); var jqbridget = require('jquery-bridget'); var react = require('react'); var likebox = require('app/likes/likes.jsx').likebox; function likeboxplugin( element, options ) { this.element = $( element ); this.options = $.extend( true, {}, this.options, options ); this._init(); } likeboxplugin.prototype.options = {}; likeboxplugin.prototype._init = function() { react.render( <likebox initialcount={this.element.data('initial-likes-count')}/>, this.element.get(0) ); }; $.bridget('likebox', likeboxplugin);
comment_box.js
"use strict"; var $ = require('jquery'); var react = require('react'); var jqbridget = require('jquery-bridget'); var commentbox = require('app/comments/comment_box.jsx').commentbox; function commentboxplugin( element, options ) { this.element = $( element ); this.options = $.extend( true, {}, this.options, options ); this._init(); } commentboxplugin.prototype.options = {}; commentboxplugin.prototype._init = function() { react.render( <commentbox contentuuid={this.element.data('uuid')} apiurl={this.element.data('api-url')}/>, this.element.get(0) ); }; $.bridget('commentbox', commentboxplugin);
to use plugins on page this:
<div> post 1 <div class="likebox' .../> <div class="commentbox' .../> </div> <div> post 2 <div class="likebox' .../> <div class="commentbox' .../> </div> <script type="text/javascript"> var $ = require('jquery'); require('app/comments/comment_box.js'); require('app/likes/likes.js'); $(function(){ $('.likebox').likebox(); $('.commentbox').commentbox(); console.log('loaded controls'); }); </script>
my problem that, after running $('.likebox').likebox();
, cannot target commentbox, eg. $('.comment-box').length
gives 0
i have tried swap around:
$(function(){ $('.commentbox').commentbox(); $('.likebox').likebox(); console.log('loaded controls'); });
i can see comment box showed briefly , got replaced box.
i have found problem, having self closing tags:
<div class="likebox' .../> <div class="commentbox' .../>
after changing to:
<div class="likebox' ...></div> <div class="commentbox' ...></div>
worked.
Comments
Post a Comment