Skip to content

Commit

Permalink
idk theme provider factory see facebook/react#3392
Browse files Browse the repository at this point in the history
  • Loading branch information
natew committed Mar 16, 2015
1 parent ea072e3 commit 54668bc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
6 changes: 4 additions & 2 deletions react-router/ParentRouteMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ module.exports = {
},

childContextTypes: {
routeDepth: PropTypes.number.isRequired
routeDepth: PropTypes.number.isRequired,
animations: PropTypes.object
},

getChildContext() {
Expand Down Expand Up @@ -43,7 +44,8 @@ module.exports = {

createChildRouteHandler(props) {
var route = this.context.router.getRouteAtDepth(this.getRouteDepth());
return route ? React.createElement(route.handler, assign({}, props || this.props, { ref: REF_NAME })) : null;
var el = route ? React.createElement(route.handler, assign({}, props || this.props, { ref: REF_NAME })) : null;
return el;
}

};
44 changes: 39 additions & 5 deletions react-router/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,42 @@ function fetchAllData(routes, params) {
return Promise.props(promises);
}

function renderToDocument(Handler, data) {
return React.render(<Handler data={data} />, document.getElementById('app'));
function renderToDocument(Handler, props, context) {
console.log(context);
var ContextHandler = ChildContextProviderFactory(context);

return React.withContext(context, () =>
React.render(
<ContextHandler componentProvider={() => <Handler {...props} />} />,
document.getElementById('app')
)
);
}

function ChildContextProviderFactory(context) {
var childContextTypes = {};
Object.keys(context).forEach(contextKey => {
childContextTypes[contextKey] = React.PropTypes.any.isRequired
});

console.log('child context types', childContextTypes, context)

return React.createClass({
displayName: 'ChildContextProvider',
childContextTypes,
propTypes: {
componentProvider: React.PropTypes.func.isRequired,
context: React.PropTypes.object.isRequired
},
getChildContext: function() {
return this.props.context;
},
render: function() {
// TODO simplify this "componentProvider hack" after React 0.14? See See https://github.com/facebook/react/issues/3392
var children = this.props.componentProvider();
return children;
}
});
}

function renderToString(Handler, data) {
Expand All @@ -33,7 +67,7 @@ module.exports = {

Router.run(routes, loc, (Handler, state) => {
fetchAllData(state.routes, state.params).then(data => {
var out = render(Handler, data);
var out = render(Handler, { data }, opts.context);

if (cb)
cb(out, data);
Expand All @@ -54,11 +88,11 @@ module.exports = {
loc = null;

Router.run(routes, loc, (Handler, state) => {
render(Handler, state);
render(Handler, { state }, opts.context);
fetchAllData(state.routes, state.params).then(data => {
// only re-render if we fetched data
if (Object.keys(data).length) {
var out = render(Handler, data);
var out = render(Handler, { data }, opts.context);

if (cb)
cb(out, data);
Expand Down

0 comments on commit 54668bc

Please sign in to comment.