I'm trying to learn meteor-react, and I have a question about using FlowRouter to insert content into HTML template pages.
Let's assume everything is imported correctly, this is the relevant code:
routes.jsx
FlowRouter.route('/post/:postId', {
name: 'posts.single',
action({postId}) {
mount(MainLayoutCtx, {
content: () => (<Post postId={postId}/>)
});
}
});
index.jsx - where MainLayoutCtx is pointing to
const Layout = ({content = () => null }) => (
//code here
);
In index.jsx, {content = () => null}. Doesn't this mean content is an object literal that has no parameters and outputs null?
But when content is being passed in routes.jsx, it's () => (/Post postId={postId}/>) So isn't that content outputting Post with postId passed in as a prop?
How does this match with what index.jsx is expecting?
In your example,
contentis a function literal with no input parameters in both cases, and it returns a new React component. (nullis a valid React component, too.)In this case
postIdis not a parameter, but a closure variable. The closure object gets created runtime embedding the value ofpostIdwhen that is of code is reached.In
index.jsxyour layout expects a function with no parameters that returns a React component, and that's exactly whatcontentis. And when you callcontent(), it creates a newPostcomponent, and passes thepostIdfrom its closure object as props.