5

I'm trying to use React-Select V2.0 with Next.JS but on server load the select element is not styled and flickers to styled.

I tried using the Emotion example for Next.JS to server render the emotion style (https://github.com/zeit/next.js/tree/master/examples/with-emotion) but it seem to conflict with Styled-JSX which I use and I get the error StyleSheet: insertRule accepts only strings.

I tried using the Emotion babel config options shown here: https://github.com/emotion-js/emotion/blob/master/docs/configurable-imports.md#babel-options

but that causes the whole page to be rendered not styled and then flickers to styled.

Does anyone know how to use React-Select V2 on Next.JS with server rendering?

Thanks.

Safinn
  • 622
  • 3
  • 15
  • 26

3 Answers3

3

Similar to other answers here, the only way I could get this working was to use a dynamic import. I would encourage you to provide some sort of loading indicator.

const ReactSelectNoSSR = dynamic(() => import('../components/select'), {
    loading: () => <Input />,
    ssr: false
});

If you need to test this component with Jest, here's how you would do that. Hope this helps.

leerob
  • 2,876
  • 1
  • 18
  • 38
2

A solution to the flickering render with React-Select v2.0 with Next.js is to render the component in client-side only using react-no-ssr

moy2010
  • 854
  • 12
  • 18
1

I currently use Next.js dynamic component loading without ssr like this:

const Select = dynamic(
  () => import('react-select').then((mod) => mod.default),
  {
    ssr: false,
    loading: () => null,
  },
);

Use select component as usual

Dziamid
  • 11,225
  • 12
  • 69
  • 104