Pass down router to child components via context -
i trying pass down router via context, can call navigateto on child react.component.
purpose:
need navigate away login when have successful login.
strategy:
1. passdown router childe login component.
2. when login call navigateto('/startpage') on router passed down.
i have on page/component contains run:
router.run(routes, function (handler) { react.render(<handler/>, document.body); });
the following context setup:
getchildcontext() { return { router: router }; }
and childcontexttypes:
childcontexttypes = { router: react.proptypes.object.isrequired };
and child itself:
contexttypes = { router: react.proptypes.object.isrequired };
the context setup works fine if using "router: react.proptypes.string.isrequired"
. note strings, when using object get:
but when try use object errors:
warning: failed context types: invalid context 'router' of type 'object' supplied 'navitemlink', expected 'function'. check render method of
and:
uncaught typeerror: this.context.router.isactive not function
any welcoming?
the solution worked me create function on parent , pass down context.
parent:
contexttypes = { router: react.proptypes.func.isrequired };
and on child components:
childcontexttypes = { router: react.proptypes.func.isrequired };
Comments
Post a Comment