[ReactNative] 프로젝트 세팅, 실행

양정훈·2022년 6월 16일
0

프로젝트 환경

  • Typescript
  • Styled Components
  • babel-plugin-root-import

프로젝트 세팅

  1. npx react-native init AwesomeProject
  2. npm install --save styled-components
  3. npm install --save-dev typescript @types/react @types/react-native @types/styled-components @types/styled-components-react-native babel-plugin-root-import
  4. cd ios && pod install && cd ..
  5. App.js → src/App.tsx
  6. index.js 수정
...
import App from '~/App';
...
  1. App.tsx 수정
import React, {Fragment} from 'react';
import {StatusBar, SafeAreaView} from 'react-native';

import {
  Header,
  LearnMoreLinks,
  Colors,
  DebugInstructions,
  ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';

import Styled from 'styled-components/native';

const ScrollView = Styled.ScrollView`
background-color: ${Colors.lighter};
`;

const Body = Styled.View`
background-color: ${Colors.white};
`;

const SectionContainer = Styled.View`
margin-top: 32px;
padding-horizontal: 24px;
`;

const SectionDescription = Styled.Text`
margin-top: 8px;
font-size: 18px;
font-weight: 400;
color: ${Colors.dark};
`;

const HighLight = Styled.Text`
font-weight: 700;
`;

interface Props {}

const App = ({}: Props) => {
  return (
    <Fragment>
      <StatusBar barStyle="dark-content" />
      <SafeAreaView>
        <ScrollView contentInsetAdjustmentBehavior="automatic">
          <Header />
          <Body>
            <SectionContainer>
              <SectionDescription>Step One</SectionDescription>
              <SectionDescription>
                Edit <HighLight>App.js</HighLight> to change this screen and
                then come back to see your edits.
              </SectionDescription>
            </SectionContainer>
            <SectionContainer>
              <SectionDescription>See Your Changes</SectionDescription>
              <SectionDescription>
                <ReloadInstructions />
              </SectionDescription>
            </SectionContainer>
            <SectionContainer>
              <SectionDescription>Debug</SectionDescription>
              <SectionDescription>
                <DebugInstructions />
              </SectionDescription>
            </SectionContainer>
            <SectionContainer>
              <SectionDescription>Learn More</SectionDescription>
              <SectionDescription>
                Read the docs to discover what to do next:
              </SectionDescription>
            </SectionContainer>
            <LearnMoreLinks />
          </Body>
        </ScrollView>
      </SafeAreaView>
    </Fragment>
  );
};

export default App;
  1. babel.config.js 수정
module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    [
      'babel-plugin-root-import',
      {
        rootPathPrefix: '~',
        rootPathSuffix: 'src',
      },
    ],
  ],
};
  1. tsconfig.json 생성
{
  "compilerOptions": {
    "allowJs": true,
      "allowSyntheticDefaultImports": true,
        "esModuleInterop": true,
          "isolatedModules": true,
            "jsx": "react",
              "lib": ["es6"],
                "moduleResolution": "node",
                  "noEmit": true,
                    "strict": true,
                      "target": "esnext",
                        "baseUrl": "./src",
                          "paths": {
                            "~/*": ["*"]
                          }
  },
    "exclude": [
      "node_modules",
      "babel.config.js",
      "metro.config.js",
      "jest.config.js"
    ]
}

프로젝트 실행

  1. react-native start --reset-cache
  2. npm run ios
  3. 안드로이드 에뮬레이터 실행
  4. npm run android

0개의 댓글