-
-
Save Happy-Ferret/a4504c1f1e68b523e91220dcbd5a5472 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Have some complicated non-React widgets that manipulate DOM? | |
// Do they manage a list of DOM elements? Here's how to wrap it | |
// into a React component so you can "constantly rerender" it. | |
// A dumb non-react widget that manually manage some DOM node that | |
// represent a list of items | |
function NonReactWidget(node) { | |
this.node = node; | |
} | |
NonReactWidget.prototype = { | |
addItem: function(item) { | |
// construct a DOM element representing the item | |
var domElement = ...; | |
this.node.appendChild(domElement); | |
} | |
removeItem: function(item) { | |
// remove the item from the list (need to find it somehow) | |
} | |
updateItem: function(item) { | |
// update the item in the list (need to find it and figure out how | |
// to update it, there's probably constraints here already since | |
// you weren't using React). You might even be able to just ignore | |
// this (with my use case I can, nothing is ever updated) | |
} | |
}; | |
// A React interface. We don't want to carefully call | |
// `addItem`/`removeItem` ourselves, we want to throw an array of | |
// items at it like we would with React components. The `Item` | |
// component is a stub that works with React's vdom system but gives | |
// us lifecycle events to actually propagate the change into the DOM. | |
const Item = React.createClass({ | |
componentDidMount: function() { | |
this.props.view.addItem(this.props.item); | |
}, | |
componentWillUnmount: function() { | |
this.props.view.removeItem(this.props.item); | |
}, | |
componentWillReceiveProps: function(nextProps) { | |
this.props.view.updateItem(nextProps.item); | |
} | |
render: function() { | |
return null; | |
} | |
}); | |
const Items = React.createClass({ | |
componentDidMount: function() { | |
this._view = new NonReactWidget(); | |
}, | |
render: function() { | |
return dom.ul( | |
null, | |
// Create a list of `Item` elements, they are stubs that let | |
// React do its diffing magic | |
dom.li(null, map(this.props.items, item => { | |
return Item({ item: item, | |
view: this._view }); | |
})) | |
); | |
} | |
}); | |
React.render(React.createElement(Items, { items: items }), | |
mountPoint); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment