Before getting started ๐
The development environment used Windows, Android Studio, and Visual Studio Code.
Let's perform the initial configuration for developing an Expo project.
We will install:
Prettier and ESLint
: Keep code style consistent and improve code quality.
Styled-components
: A CSS-in-JS library for styling components.
Recoil
: A state-management library for global state.
Installing ESLint and Prettier ๐
Install ESLint in the Expo project with the following command.
The first run generates
.eslintrc.js
, the ESLint configuration file.
Later runs of the same command display lint results.
Edit
.eslintrc.js
to use it alongside Prettier.
.eslintrc.js module . exports = {
extends: [ 'expo' , 'prettier' ],
plugins: [ 'prettier' ],
rules: {
'prettier/prettier' : 'error' ,
},
};
Create
.eslintignore
with the following contents to ignore unnecessary files.
.eslintignore
Run the following command in the project directory.
npx expo install -- --save-dev prettier eslint-config-prettier eslint-plugin-prettier
Create
.prettierrc.js
in the project folder and add this configuration.
.prettierrc.js module . exports = {
singleQuote: true , // ๋ฌธ์์ด์ ํ๋ฐ์ดํ๋ก
parser: "typescript" , // ํ์
์คํฌ๋ฆฝํธ ์ฌ์ฉ
semi: true , // ์ธ๋ฏธ์ฝ๋ก ์ฌ์ฉ
useTabs: false , // ํญ ์ฌ์ฉ ๊ธ์ง
tabWidth: 2 , // ๋ค์ฌ์ฐ๊ธฐ 2์นธ
printWidth: 120 , // ์ค ๋ฐ๊ฟ ๊ธธ์ด
arrowParens: "always" , // ํ์ดํ ํจ์ ๊ดํธ ์ฌ์ฉ
};
To exclude files from formatting, create
.prettierignore
with the following contents.
.prettierignore
Saving a file now lets Prettier format it automatically according to the ESLint configuration.
Installing Styled-components ๐
Using styled-components in Expo works the same way as in a React Native CLI project.
Install it with the following command.
Also install its React Native type definitions.
npm i @types/styled-components @types/styled-components-react-native
Styled-components example ๐
Update App.tsx to use styled-components.
App.tsx import React from 'react' ;
import { Text, View } from 'react-native' ;
import styled from 'styled-components/native' ;
export default function App () {
return (
< Container >
< Title >Hello, Expo!</ Title >
</ Container >
);
}
const Container = styled. View `
flex: 1;
justify-content: center;
align-items: center;
` ;
const Title = styled. Text `
font-size: 24px;
font-weight: bold;
` ;
You can now use styled-components.
Install Recoil in the Expo project with the following command.
Add RecoilRoot to app/_layout.tsx.
app/_layout.tsx import React from 'react' ;
import { RecoilRoot } from 'recoil' ;
export default function App () {
return (
< ThemeProvider value ={ colorScheme === 'dark' ? DarkTheme : DefaultTheme } >
< RecoilRoot >
< Stack >
< Stack.Screen name = "(tabs)" options ={ { headerShown: false } } />
< Stack.Screen name = "+not-found" />
</ Stack >
</ RecoilRoot >
</ ThemeProvider >
);
}
You can now manage global state with Recoil.
First, create an atom.
atoms/counter.ts import { atom } from 'recoil' ;
export const counterState = atom ({
key: 'counterState' , // ๊ณ ์ ํ ํค
default: 0 , // ์ด๊ธฐ๊ฐ
});
Then access the global state from a component.
Recoil's basic usage resembles useState.
components/Counter.tsx import React from 'react' ;
import { Text, Button } from 'react-native' ;
import { useRecoilState } from 'recoil' ;
import { counterState } from '../atoms/counter' ;
export default function Counter () {
const [ counter , setCounter ] = useRecoilState (counterState);
return (
<>
< Text > { counter } </ Text >
< Button title = "Increase" onPress ={ () => setCounter (counter + 1 ) } />
</>
);
}
ESLint: Delete `cr` in VS Code ๐
The error above can appear when using ESLint in VS Code.
It is caused by Windows line-ending characters.
Adjust
.eslintrc.js
as follows to resolve it.
.eslintrc.js module . exports = {
extends: [ 'expo' , 'prettier' ],
plugins: [ 'prettier' ],
rules: {
'prettier/prettier' : [ 'error' , { endOfLine: 'auto' }],
},
};
We configured Prettier, ESLint, Styled-components, and Recoil in an Expo project.
The project is now ready for development.