Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Select V2.0 with Next.JS

Tags:

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.

like image 992
Safinn Avatar asked Jun 19 '18 16:06

Safinn


2 Answers

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.

like image 156
leerob Avatar answered Sep 28 '22 17:09

leerob


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

like image 36
moy2010 Avatar answered Sep 28 '22 19:09

moy2010