0

I've just started to play around with this package that makes it super easy to integrate SSR.

https://github.com/Meteor-Community-Packages/react-router-ssr

The thing is I do not know how to access the route/query params or such url data from within the server component, and therefore I can't render a specific content. So for the code below, I don't know how I could inject the documentId into the Sample server component. It is probably a specific react-router syntax injecting as a prop...?

TLDR: Since I can't use hooks hence the builtin useParams hook, how do I get access to the param in the server component?

Thanks!

function Sample(props) {
    Meteor.subscribe('document', 'documentId');
    const document = Documents.findOne();
    return <h1>{document.title}</h1>
}

const AppRoutesSSR = [{
    path: '/documents/:documentId'.
    element: <Sample documentId='???documentId???' />
}, {
    ...
}]

renderWithSSR(AppRoutesSSR, {
  renderTarget: 'root',
});
2
  • 1
    If you are trying to access the documentId route path parameter then you'd need to do that in the routed component, e.g. Sample component in this case. Is there any reason why Sample can't use the useParams hook client side? Does Meteor have some method of reading route params server-side?
    – Drew Reese
    Commented Jun 8 at 18:30
  • Apparently I didn't get it quite right that one could not use hooks in server components at all. So you are right: useParams works and is the answer.
    – Emo
    Commented Jun 8 at 20:57

1 Answer 1

1

Indeed it is apparently possible to use hooks, specifically the useParams hook from react router itself.

So:

import { useParams } from 'react-router-dom'

function Sample() {
    const { documentId } = useParams();
    Meteor.subscribe('document', documentId);
    const document = Documents.findOne(documentId);
    return <h1>{document.title}</h1>
}

Bamm!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.