Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What are the advantages of using 'injectDeps' to load the layout into the route? #145

Open
THPubs opened this issue Mar 16, 2016 · 4 comments

Comments

@THPubs
Copy link

THPubs commented Mar 16, 2016

Earlier, without using Mantra, I used this method to load the layouts :

FlowRouter.route('/', {
  name: 'home',
  action () {
    mount(MainLayout, { content: (<Home />) });
    setTitle();
  },
});

But when following a tutorial, I saw they using injectDeps to load the layout. What is the difference and what's the most preferred method?

export default function (injectDeps, {FlowRouter}) {
  const MainLayoutCtx = injectDeps(Layout);
  FlowRouter.route('/', {
    name: 'items.list',
    action() {
      mount(MainLayoutCtx, {
        content: () => (<ItemList />)
      });
    }
  });
}
@fermuch
Copy link

fermuch commented Apr 6, 2016

The only difference is that with injectDeps you have the context available in your Layout.

@jkhoffman
Copy link

Which context? The application context defined in client/configs/context.js? How do I access it from my layout?

I'm trying to get the current route from FlowRouter in my layout. Can't figure out how to get FlowRouter from the app context in my layout component.

@v-anyukov
Copy link

U can get context from first argument in composer function in your container, like so https://github.com/mantrajs/mantra-sample-blog-app/blob/master/client/modules/core/containers/postlist.js#L5 , and get FlowRouter (or Meteor, or Collections etc) from object returned by context() call.

@jkhoffman
Copy link

jkhoffman commented Dec 5, 2016

Yeah, I knew how to access the application context from the container, but I could not figure out how to access it from my main layout template.

Last night, I finally figured it out.

const AppLayout = ({ content }, reactContext) =>
  <main className={reactContext.context.FlowRouter.current().route.name}>
    {content()}
  </main>;

AppLayout.contextTypes = {
  context: PropTypes.object,
};

AppLayout.propTypes = {
  content: PropTypes.func,
};

The key was to define AppLayout.contextTypes. Once that is defined, the React context gets passed as the second parameter to the layout component. Mantra's app context gets passed inside that, as simply 'context'. So, reactContext.context is the Mantra app context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants