slorber / gatsby-plugin-react-native-web

react-native-web plugin for Gatsby

Home Page:https://sebastienlorber.com/using-expo-in-gatsby

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

styles that depend dimension/layout APIs

brianjd opened this issue · comments

commented

I have styles that depend upon the dimension/layout API. In my gatsby-ssr/onRenderBody, I have been calling Dimensions.set (as per necolas/react-native-web#1225) with mobile dimensions, taking a 'mobile-first' approach to styling/rendering. In addition, my components are wrapped in a dimensions aware context/consumer. The issue is that the mobile styles are cached when accessed on desktop. I tried to remedy this by calling Dimensions._update from gatsby-browser/onInitialClientRender, but this did not trigger style re-calculation.

So the question, what is the best approach to set initial dimensions for ssr rendering?

commented

Hi Sébastien,

Maybe not a problem specifically with the plugin itself, but definitely something other users of this plugin will encounter when using the Dimensions API. I thought there may be a better way of hooking into the Gatsby SSR and Browser API lifecycle hooks to get things working as expected.

The dimension API is listener based. A call to Dimensions._update fires the listeners as well. I have the listener attached in my 'dimensions aware context'. It fires, just styles don't update.

I guess I can just use regular CSS w/ media-queries for styling components that require platform specific responsive attributes. I was trying to keep things pure, and in congruence with RNW best practices.

commented

I could also setup multiple build targets in Gatsby (mobile, tablet, desktop). In each build pre-render call Dimensions.set with the average dimensions for the target platform. Since I'm using S3/Cloudfront for delivery, I can then use Cloudfront device type detection and then route the appropriate build / origin. Or maybe I'm just complicating things 😅

Yeah don't know if this would always be safe but you can try