Closed SzymonGalazka closed 1 year ago
I haven't gotten far in implementing an app with RSC, but I believe in this construct, react-responsive would fit best into a "use client" component.
One of the things about RSC is that they don't really have a finalized conceptual model for doing pre-rendering / static generation.
I'm not sure if there is a way in Next 13 to provide context to client-only components. In Gatsby there is a wrapRootElement
API in gatsby-browser.js
that would work well for this concept.
It looks like you are using next.js - you can use use client
or you can do:
import dynamic from "next/dynamic"
const MediaQuery = dynamic(() => import("react-responsive"), {
ssr: false
})
Added this to the docs, closing.
Hi all, as React team has created the concept of Server Components, current
react-responsive
approach to rendering on the server is no longer working. Before the introduction of Server Components, you could use the Provider to supplyreact-responsive
hooks withwidth
prop on the server. I personally calculated the width based on User Agent data but it could've been placeholder value as well:with Server Components,
Provider
cannot be used on the server at all:You can test that behaviour in Next 13 which allows the use of Server Components.
You can bypass this by creating a client-side wrapper as described here but this only shifts the initialization of library to the client. You lose the benefit of having
react-responsive
on the server. Besides that, all components that usereact-responsive
in any way need to be client components too.It will be nice to have the option to still use
react-responsive
and components that use it, on the server. Maybe usingimport { createServerContext } from 'react'
?