Last active
June 28, 2018 21:59
-
-
Save StevenLangbroek/ff11f1c556d9be8ce9fc3f327f2997ff to your computer and use it in GitHub Desktop.
Parameterized routing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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