Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Using Knockout and React together. Hook React component into Knockout.
define(
['log', 'knockout', 'react', 'react-dom'],
function (log, ko, React, ReactDOM) {
"use strict";
function KnockoutReactBinding() {
log.info('Loading KO binding for React');
this.init();
}
KnockoutReactBinding.prototype.init = function () {
/**
* This binding helps loading react component within KO space.
* KO wont clobber this component and its descendants.
* USAGE: <div data-bind="react: IronBankApp"></div>
*/
ko.bindingHandlers.react = {
init: function () {
return {controlsDescendantBindings: true};
},
update: function (el, valueAccessor, allBindings) {
var Component = ko.unwrap(valueAccessor());
var props = ko.toJS(allBindings.get('props'));
// Tell react to render/re-render
ReactDOM.render(React.createElement(Component, props), el);
}
};
};
return new KnockoutReactBinding();
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment