Safinn
Safinn

Reputation: 632

React-Select V2.0 with Next.JS

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.

Upvotes: 5

Views: 9967

Answers (3)

leerob
leerob

Reputation: 3122

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.

Upvotes: 5

Dziamid
Dziamid

Reputation: 11579

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

Upvotes: 2

moy2010
moy2010

Reputation: 903

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

Upvotes: 2

Related Questions