Skip to content

Instantly share code, notes, and snippets.

@StevenLangbroek
Last active June 28, 2018 21:59
Show Gist options
  • Save StevenLangbroek/ff11f1c556d9be8ce9fc3f327f2997ff to your computer and use it in GitHub Desktop.
Save StevenLangbroek/ff11f1c556d9be8ce9fc3f327f2997ff to your computer and use it in GitHub Desktop.
Parameterized routing
import React from 'react';
import { withParams } from 'next/router';
function Community(props) {
const { params, community, channel } = props;
if (Boolean(params.channelId)) {
return <ChannelPage channel={channel} />
}
if (Boolean(params.communityId)) {
return <CommunityPage community={community} />
}
return <FeaturedCommunities />
}
Community.getInitialProps = async ({ params }) => {
const { communityId, channelId } = params;
let community, channel;
// fetch community & channel if their params are present.
// So if /communities/styled-components, just the community,
// if it includes a channelId then also fetch that.
// Or don't and leave it up to the component you render above
// with a <Query /> component or some such.
return { community, channel };
}
export default withParams('communityId', 'channelId')(Community);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment