0

I have a component which renders the response (Successfully verified, Has been verified already, and this token has expired) from making a call to my server from clicking a link in a Account Verification email I sent after registering.

At very rare instances the UI would render, but the content for it wouldn't, so I was reading the Next docs regarding getStaticProps and getStaticPaths and figured pre-rendering is what i need \o/

Right now I am using the useEffect hook in the component:

function isConfirmationForm() {
  useEffect(() => {
    axios
      .get(`/users/confirmation/${match.params.token}`)
      .then(response => {
        if (response.status === 200) {
          setError(false);
          setResponseMessage(response.data.msg);
        }
      })
      .catch(function(error) {
        if (error.response.status === 404) {
          resetUserAcoountVerified();
          setResponseMessage(error.response.data.msg);
          setError(true);
        }
        if (error.response.status === 400) {
          userHasBeenVerified();
          setResponseMessage(error.response.data.msg);
          setError(true);
        }
      });
  }, []);

  const isNull = value => typeof value === 'object' && !value;
  return (
    <div className="login-form">
      {error === false ? (
        <Transition unmountOnHide={true} animation="scale" duration={duration}>
          <Message success header={responseMessage[0]} />
        </Transition>
      ) : (
        ''
      )}

      {accountNotVerified === false && error === true ? (
        <Transition unmountOnHide={true} animation="scale" duration={duration}>
          <Message error header={responseMessage[0]} />
        </Transition>
      ) : (
        ''
      )}
      {isNull(accountNotVerified) && error === true ? (
        <Transition unmountOnHide={true} animation="scale" duration={duration}>
          <Message error header={responseMessage[0]} />
        </Transition>
      ) : (
        ''
      )}
    </div>
  );
}

But what I want to do now is fill this bad-boy with pre-rendered data,

import ConfirmationPage from '../components/FormComponent/FormComponent.jsx';
import { withRouter } from 'react-router-dom';

const Confirmation = props => (
  <>
    <ConfirmationPage formType="Confirmation" {...props} />
  </>
);

export async function getStaticPaths() {
  return {
    paths: [
      { params: { token: match.params.token } } 
    ],
    fallback: false 
  };
}

export async function getStaticProps({ params }) {

  return { props: {.... } };
}

export default Confirmation;

I'm stuck at how to approach it because on the component level as you can see I've created some hooks which are reacting (ha!) to the axios call. I am not sure how to wire the getStaticProps Reading the docs it feels like you need to use getStaticPaths with it....

Any help would be appreciated!

Antonio Pavicevac-Ortiz
  • 7,239
  • 17
  • 68
  • 141
  • 1
    Why would you want to use `react-router-dom` with nextjs. Nextjs has it's own filesystem-based routing if it doesn't meet your requirement you can add a custom server which will handle the routing. I suggest you read through the [routing section](https://nextjs.org/docs/routing/introduction) and the [data-fetching section](https://nextjs.org/docs/basic-features/data-fetching) in the docs. – subashMahapatra Jun 30 '20 at 19:50
  • and also note that if you use next.js 9.4 you dont need axios since it has built-in fetch polyfill – Nico Jun 30 '20 at 21:58
  • @subashMahapatra File based routing didn’t work with a use case I had with Semantic-ui-react – Antonio Pavicevac-Ortiz Jun 30 '20 at 22:14
  • @Nico thanks for a comment that was helpful, I didn’t know that as I was using 9.3.xx – Antonio Pavicevac-Ortiz Jun 30 '20 at 22:15
  • 1
    `getStaticProps` and `getStaticPaths` are used when data required to render the page is available **at build time** ahead of a user’s request. ConfirmationPage may not fit the use case since the confirmation status will probably change after the pages are built. In that case, `getServerSideProps` is more suitable since it is for pages whose data must be fetched **at request time**. All these methods can only be used in page components. I recommend you to stick with client-side `useEffect` and try to debug why "At very rare instances the UI would render, but the content for it wouldn't". – hangindev.com Jul 01 '20 at 02:58
  • 1
    Regarding the mix of use of `react-router` and `next/router`, I am not sure if it is a good idea to have two routers interacting with the browser history API at the same time. It may be a valid use case but from my experience, it is very likely that Next's file-system based router has covered most if not all of the use cases. – hangindev.com Jul 01 '20 at 03:07
  • @Hangindev Well actually I ripped out all instances of `next/router` So now I'm using just RR. If you wanted to check out what was happening this is a question I posted last [november](https://stackoverflow.com/questions/58653701/imperative-version-of-next-router-not-working-with-semantic-ui-react) :) – Antonio Pavicevac-Ortiz Jul 01 '20 at 03:37
  • I see. For a top Nav bar like that which you want it to be mounted all the time (not unmount and remount during page transition), I usually create a Layout component and put it in `_app.js` which let you have persisting layout between page changes. [docs](https://nextjs.org/docs/advanced-features/custom-app) – hangindev.com Jul 01 '20 at 03:48

0 Answers0