-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[bugfix] fix imports for react-native-web #2797
[bugfix] fix imports for react-native-web #2797
Conversation
React Native Web doesn't use default exports, so importing using ES6 imports breaks it for that react-native renderer. Using require creates the same behavior in react-native and react-native-web without tripping up the navigator invariant that pops up when trying to import * as reactNative from 'react-native'.
LGTM but I'd like to publish it as a test version for people to try out before we merge into master |
Try |
Worked for me! Steps:
|
This worked perfectly for me 🙌❤️ |
@FiberJW can you try with this: import reactNative from 'react-native'
const { StyleSheet } = reactNative; |
Doesn't work with SSR, for example Next.js |
@danilobrinu
https://github.com/necolas/react-native-web/blob/master/packages/react-native-web/src/index.js |
@daniellmartins you're using Next.js+React Native Web+Styled Components? Not sure how because it doesn't work on master. This doesn't break any functionality with Next.JS; it creates the possibility to use it with Next.JS. |
@FiberJW can you add a changelog entry and a similar PR for canary? thanks! |
done 👍 |
Now that I look at it, this fix is already on Canary. I closed my PR accordingly. Thanks! |
But what about @types/styled-components? I still have bunch of errors on backend: UPD: it's still not fixed, for now you can use: skipLibCheck: false for tsconfig. Issue: 33015
|
@daniellmartins I'm experiencing the same problem, @FiberJW 's solution doesn't work with @expo/next-adapter. Were you able to find a solution? |
While styled-components/native "works" with react-native web in it's curren't implementation, it adds a SIGNIFICAN'T chunk to the final bundle due to the way react-native is imported. Basically the require + this:
I would wager that most developers only use a fraction of react-native-web, but if you include ANY reference to styled-components/native it adds 200-300kb to the gzipped bundle. My question though is if we could do this in a smarter way?
I'm happy to do the work on this, I just wanted to check if anyone had the answers to the above before I jump into it. |
I think people would love for someone to figure out how to get tree-shaking working correctly. I don't currently have a solution for it and not much bandwidth to allocate towards such a feat, so I fully invite you to take on the challenge if you're up for it! This patch was just to get SC with RNW from broken to working, and it could definitely improve from here 👍 |
Just wondering, has anyone figured out a solution to this problem? |
@nandorojo yes |
@MrLoh do you import styled from native, or normal styled components? |
yes: https://github.com/christophehurpeau/babel-plugin-styled-components-react-native-web |
Hello 👋🏾,
This bug surfaced as a result of trying to use styled-components in an Expo universal project (React Native + React Native Web).
react-native-web
doesn't use default exports likereact-native
official does, so importing using ES6 default imports breaks it for that react native renderer (TypeError: Cannot read property 'View' of undefined
). Usingrequire
creates the desired behavior both inreact-native
andreact-native-web
without tripping up thenavigator is deprecated and has been removed from this package.
invariant that pops up when trying to useimport * as reactNative from 'react-native'
.It's a pretty simple fix and all of the tests passed, so I hope that this is easy to review and possibly merge.
Thanks!
fixes #2624
-- Juwan Wheatley (@FiberJW)