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

docs: Add information about Expo and usage with Expo and Next.js #928

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion sections/basics/react-native.mdx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## React Native

styled-components can be used with React Native in the same way and with the
same import. Try this example with [Snack by Expo](https://snack.expo.io/@danielmschmidt/styled-components).
same import. Try this example with [Snack by Expo](https://snack.expo.dev/@danielmschmidt/styled-components).

```tsx
import React from 'react'
Expand Down Expand Up @@ -58,3 +58,38 @@ nest your CSS.
### Simpler usage with the metro bundler

If you'd prefer to just import `styled-components` instead of `styled-components/native`, you can add a [`resolverMainFields` configuration](https://facebook.github.io/metro/docs/configuration#resolvermainfields) that includes `"react-native"`. This used to be supported in metro by default (and currently does work in haul) but appears to have been removed at some point.

## Expo

styled-components can be used in an [Expo](https://github.com/expo/expo) project the same way in a React Native project for native platforms.

### Usage with Expo and Next.js

Using Expo and Next.js to create a [universal app](https://docs.expo.dev/workflow/web/) is a bit different. You'll need to apply the React Native aliases manually. This is done by `@expo/webpack-config` (which is in [`@expo/next-adapter`](https://github.com/expo/expo-cli/tree/main/packages/next-adapter#readme)).

- Add `@expo/next-adapter` to your project:

```sh
npx @expo/next-adapter
```

- Install the styled-components Babel plugin:

```sh
$ npm install -D babel-plugin-styled-components
```

- Use the Babel plugin in your **babel.config.js** file:

```diff
module.exports = {
presets: ['@expo/next-adapter/babel'],
+ plugins: [['styled-components', { ssr: true }]]
}
```

Now you can use `styled-components/native` just like in a regular Expo project!

### Tree-Shaking

styled-components imports all of `react-native-web` which [breaks React Native web tree-shaking](https://github.com/styled-components/styled-components/pull/2797#issuecomment-574955289). This means your bundle size will be larger and include all of the components exported from `react-native-web`.
1 change: 1 addition & 0 deletions test/components/NavBar/__snapshots__/Navbar.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ exports[`Navbar renders correctly 1`] = `
align-items: center;
background-color: rgba(12, 13, 15, 0.7);
backdrop-filter: blur(5px);
-webkit-backdrop-filter: blur(5px);
box-sizing: border-box;
color: white;
display: flex;
Expand Down
1 change: 1 addition & 0 deletions test/components/NavBar/__snapshots__/index.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ exports[`Nav renders correctly 1`] = `
align-items: center;
background-color: rgba(12, 13, 15, 0.7);
backdrop-filter: blur(5px);
-webkit-backdrop-filter: blur(5px);
box-sizing: border-box;
color: white;
display: flex;
Expand Down
1 change: 1 addition & 0 deletions test/components/__snapshots__/DocsLayout.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ exports[`DocsLayout renders correctly 1`] = `
align-items: center;
background-color: rgba(12, 13, 15, 0.7);
backdrop-filter: blur(5px);
-webkit-backdrop-filter: blur(5px);
box-sizing: border-box;
color: white;
display: flex;
Expand Down