Skip to content
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

feat: add basic support for React Server Components #2678

Closed

Conversation

EvanBacon
Copy link
Contributor

I'm adding React Server Components support to Expo Router, and need basic support added to React Native Web. This PR contains a cleaned up version of the patches I've been developing against for the last couple months. I just open sourced this branch which can be used to verify support: expo/expo#29404

Here are a few of the migration principles:

  • useState and other dynamic hooks cannot be used in server components.
  • createContext is not available in a react-server environment.
  • Component and PureComponent are not allowed in react-server.
  • React.Children.toArray appears to clobber transitional react elements. This caused a lot of headache since we perform the check in <View />
  • You cannot dot-access into client modules. This wasn't a huge issue with rnw since most of the modules are already optimized for the server, but in react-native this means any use of StyleSheet.create would be invalid, since StyleSheet is naively (by me) marked as a client module.

We can (and probably should) circle back and optimize the modules even more for react-server environments, but basic support is substantially nicer than no support.

Copy link

codesandbox-ci bot commented Jun 3, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit b0dda5b:

Sandbox Source
react-native-web-examples Configuration

@@ -6,6 +6,7 @@
*
* @flow strict
*/
'use client';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this move to exports/useLocaleContext?

@@ -7,6 +7,8 @@
* @flow
*/

'use client';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this module need use client?

@@ -8,6 +8,8 @@
* @format
*/

'use client';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this move to exports/FlatList?

kkafar pushed a commit to software-mansion/react-native-screens that referenced this pull request Sep 25, 2024
## Description

Add basic support for React Server Components support.

Related PRs for more context:

- necolas/react-native-web#2678
- th3rdwave/react-native-safe-area-context#502
- software-mansion/react-native-svg#2287

## Changes

- Added basic React Server Component support

## Test code and steps to reproduce

- It's kinda hard to test this E2E. I wrote a small jest runner in
jest-expo which executes in RSC mode, but it requires React 19 to work.
Here's where I tested this patch expo/expo#29404
- It will likely break between now and the full release, but this should
at least reduce the number of patches required to work on Expo Router
support.
- There's pretty good compilation on web:
```
2:I["../../node_modules/react-native-web/dist/exports/View/index.js",[],""]
3:I["../../node_modules/react-native-web/dist/exports/Image/index.js",[],""]
1:{"name":"ScreenStackHeaderBackButtonImage","env":"Server","owner":null}
0:D"$1"
0:["$","$L2",null,{"children":["$","$L3",null,{"resizeMode":"center","fadeDuration":0},"$1"]},"$1"]
```
- And more opaque compilation on native platforms:
```
1:I["../../node_modules/react-native-screens/src/components/ScreenStackHeaderConfig.tsx",[],"ScreenStackHeaderBackButtonImage"]
0:["$","$L1",null,{},null]
```


## Checklist

- [ ] Included code example that can be used to test this change
- [ ] Updated TS types
- [ ] Updated documentation: <!-- For adding new props to native-stack
-->
- [ ]
https://github.com/software-mansion/react-native-screens/blob/main/guides/GUIDE_FOR_LIBRARY_AUTHORS.md
- [ ]
https://github.com/software-mansion/react-native-screens/blob/main/native-stack/README.md
- [ ]
https://github.com/software-mansion/react-native-screens/blob/main/src/types.tsx
- [ ]
https://github.com/software-mansion/react-native-screens/blob/main/src/native-stack/types.tsx
- [ ] Ensured that CI passes
ja1ns pushed a commit to WiseOwlTech/react-native-screens that referenced this pull request Oct 9, 2024
## Description

Add basic support for React Server Components support.

Related PRs for more context:

- necolas/react-native-web#2678
- th3rdwave/react-native-safe-area-context#502
- software-mansion/react-native-svg#2287

## Changes

- Added basic React Server Component support

## Test code and steps to reproduce

- It's kinda hard to test this E2E. I wrote a small jest runner in
jest-expo which executes in RSC mode, but it requires React 19 to work.
Here's where I tested this patch expo/expo#29404
- It will likely break between now and the full release, but this should
at least reduce the number of patches required to work on Expo Router
support.
- There's pretty good compilation on web:
```
2:I["../../node_modules/react-native-web/dist/exports/View/index.js",[],""]
3:I["../../node_modules/react-native-web/dist/exports/Image/index.js",[],""]
1:{"name":"ScreenStackHeaderBackButtonImage","env":"Server","owner":null}
0:D"$1"
0:["$","$L2",null,{"children":["$","$L3",null,{"resizeMode":"center","fadeDuration":0},"$1"]},"$1"]
```
- And more opaque compilation on native platforms:
```
1:I["../../node_modules/react-native-screens/src/components/ScreenStackHeaderConfig.tsx",[],"ScreenStackHeaderBackButtonImage"]
0:["$","$L1",null,{},null]
```


## Checklist

- [ ] Included code example that can be used to test this change
- [ ] Updated TS types
- [ ] Updated documentation: <!-- For adding new props to native-stack
-->
- [ ]
https://github.com/software-mansion/react-native-screens/blob/main/guides/GUIDE_FOR_LIBRARY_AUTHORS.md
- [ ]
https://github.com/software-mansion/react-native-screens/blob/main/native-stack/README.md
- [ ]
https://github.com/software-mansion/react-native-screens/blob/main/src/types.tsx
- [ ]
https://github.com/software-mansion/react-native-screens/blob/main/src/native-stack/types.tsx
- [ ] Ensured that CI passes
necolas pushed a commit that referenced this pull request Oct 16, 2024
Close #2678
Fix #2672

Co-authored-by: evanbacon <bacon@expo.io>
@necolas necolas closed this in 1d4568d Oct 16, 2024
jakex7 pushed a commit to software-mansion/react-native-svg that referenced this pull request Oct 18, 2024
# Summary

Add basic support for React Server Components support. This package
could surely be improved (at least on web) but there are a lot of class
components (unsupported in RSC) which need to be rewritten to function
components in order to compile down further. For now, I'm opting the
entire package.

Related PRs for more context:

- necolas/react-native-web#2678
- th3rdwave/react-native-safe-area-context#502
- I've added more support for mixing export types, but it isn't perfect
expo/expo#29426

## Test Plan

- It's kinda hard to test this E2E. I wrote a small jest runner in
jest-expo which executes in RSC mode, but it requires React 19 to work.
Here's where I tested this patch expo/expo#29404
kkafar pushed a commit to software-mansion/react-native-screens that referenced this pull request Oct 25, 2024
## Description

Add basic support for React Server Components support.

Related PRs for more context:

- necolas/react-native-web#2678
- th3rdwave/react-native-safe-area-context#502
- software-mansion/react-native-svg#2287

## Changes

- Added basic React Server Component support

## Test code and steps to reproduce

- It's kinda hard to test this E2E. I wrote a small jest runner in
jest-expo which executes in RSC mode, but it requires React 19 to work.
Here's where I tested this patch expo/expo#29404
- It will likely break between now and the full release, but this should
at least reduce the number of patches required to work on Expo Router
support.
- There's pretty good compilation on web:
```
2:I["../../node_modules/react-native-web/dist/exports/View/index.js",[],""]
3:I["../../node_modules/react-native-web/dist/exports/Image/index.js",[],""]
1:{"name":"ScreenStackHeaderBackButtonImage","env":"Server","owner":null}
0:D"$1"
0:["$","$L2",null,{"children":["$","$L3",null,{"resizeMode":"center","fadeDuration":0},"$1"]},"$1"]
```
- And more opaque compilation on native platforms:
```
1:I["../../node_modules/react-native-screens/src/components/ScreenStackHeaderConfig.tsx",[],"ScreenStackHeaderBackButtonImage"]
0:["$","$L1",null,{},null]
```

## Checklist

- [ ] Included code example that can be used to test this change
- [ ] Updated TS types
- [ ] Updated documentation: <!-- For adding new props to native-stack
-->
- [ ]
https://github.com/software-mansion/react-native-screens/blob/main/guides/GUIDE_FOR_LIBRARY_AUTHORS.md
- [ ]
https://github.com/software-mansion/react-native-screens/blob/main/native-stack/README.md
- [ ]
https://github.com/software-mansion/react-native-screens/blob/main/src/types.tsx
- [ ]
https://github.com/software-mansion/react-native-screens/blob/main/src/native-stack/types.tsx
- [ ] Ensured that CI passes

(cherry picked from commit 75c15c1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants