javascript - Can I use Backbone with React Native? -
i have found several different examples of integrating react (view) backbone on web, not same react native. wondering if possible , if there samples play with.
this (https://github.com/magalhas/backbone-react-component) seemed starting point, since don't have knowledge, not sure if can still used in react native.
thanks.
you'll able use parts of backbone, yes.
backbone's views operate on dom in web browser, , since react native doesn't have that, you'll unable use backbone view as-is.
however, notice backbone.react.component described "a mixin glues backbone models , collections react components". therefore works data layer of application, supplying data view.
this useful in theory, in practice i've tried , doesn't work! said, did managed tweak code of backbone.react.component fix though, , demo works:
'use strict'; var react = require('react-native'); var backbone = require('backbone'); var backbonemixin = require('backbone-react-component'); var { appregistry, stylesheet, text, view, } = react; var mycomponent = react.createclass({ mixins: [backbonemixin], render: function () { return <text>{this.state.model.foo}</text> } }); var model = new backbone.model({foo: 'bar'}); model.set('foo', 'hello world!'); var reactnativebackbone = react.createclass({ render: function() { return ( <view> <mycomponent model={model} /> </view> ); } }); appregistry.registercomponent('reactnativebackbone', () => reactnativebackbone);
you see value of foo
model shown on screen "hello world!". need edit lib/component.js in backbone.react.component this:
diff --git a/node_modules/backbone-react-component/lib/component.js b/fixed.js index e58dd96..0ca09f7 100644 --- a/node_modules/backbone-react-component/lib/component.js +++ b/fixed.js @@ -32,7 +32,7 @@ if (typeof define === 'function' && define.amd) { define(['react', 'backbone', 'underscore'], factory); } else if (typeof module !== 'undefined' && module.exports) { - module.exports = factory(require('react'), require('backbone'), require('underscore')); + module.exports = factory(require('react'), require('backbone'), require('underscore')); } else { factory(root.react, root.backbone, root._); } @@ -70,11 +70,11 @@ }, // sets `this.el` , `this.$el` when component mounts. componentdidmount: function () { - this.setelement(react.finddomnode(this)); + //this.setelement(react.finddomnode(this)); }, // sets `this.el` , `this.$el` when component updates. componentdidupdate: function () { - this.setelement(react.finddomnode(this)); + //this.setelement(react.finddomnode(this)); }, // when component gets initial state, instance `wrapper` take // care of models , collections binding `this.state`.
i made 2 changes:
- require
react
instead ofreact
- remove references
finddomnode
i haven't done extensive testing should started. i've opened issue try , problem addressed in main backbone.react.component code base.
Comments
Post a Comment