javascript - React.js with CoffeeScript - passing props and methods to child component -
i working react.js using coffeescript, makes impossible me use jsx (or it?).
i use global namespace _ store controllers , views etc.
i have modal controller creates modal wrapper , accepts content (another react view component).
class modal constructor : (@react) -> @wrapper = $('#modal') create : (@content, @data, @callback) -> # save _this = @ # create modal wrapper wrapperview = @react.createclass getinitialstate : -> visible : false # after mounting componentdidmount : -> // not important code componentdidupdate : -> // not important code componentwillunmount : -> # remove esc event _.event.lose 'closemodal' close : -> @setstate visible : false render : -> _.react.div 'classname' : 'modal', _this.react.createelement @props.content, data : @props.data, close : @close # renders modal window prepared wrapper @react.render (@react.createelement wrapperview, { content : @content, data : @data }), @wrapper.get 0
what worries me bit line
_this.react.createelement @props.content, data : @props.data, close : @close
@props.content react component passed modal controller , (lets _.views.form). pass modals methods such close need list them when create them.
is there no way child element access parents methods , props without them being implicitly passed?
would bad practice below , pass parent has child element?
_this.react.createelement @props.content, @
if your'e passing initialized react element, should rendering it, not creating new one. that's done so:
react.render(element, container, callback);
if want create new one, you'd use react.cloneelement
depending on version. clone element can pass element's props second parameter, or in earlier versions of react there deprecated method called clonewithprops
.
and if want methods parent accessible in child, must pass them or alternatively use flux pattern call action child.
Comments
Post a Comment