본문 바로가기
언어공부/JS&TS&React

[React 생활코딩] 1. 소개 및 개발환경

by hobbiz 2021. 6. 2.
반응형

리액트의 장점

  1. 가독성
  2. 재사용성
  3. 유지보수

 

컴포넌트를 중심으로 현대적인 웹애플리케이션 UI를 만들 수 있다.

 

Coding -> Run -> Deploy 의 순서로 배울 것

 

React 공식문서 홈페이지 활용하기

https://reactjs.org 

 

 

실습을 위한 개발환경 셋팅

1. Nodejs 설치파일 다운로드 후 실행

https://nodejs.org/ko/

 

2. npm 이용하여 Create React App 설치

cmd창에서 npm -v 입력 후 버전확인

npm install -g create-react-app 입력하여 설치 후 

create-react-app -v 입력하여 버전확인

 

 

3. react-app 디렉토리 설정 및 실행

react-app 디렉토리 생성

cd C:\react-app 입력하여 해당 디렉토리로 이동

create-react-app . 입력하여 환경설정

 

 

참고: Create React App

더보기

Create React App React 배우기에 간편한 환경입니다. 그리고 시작하기에 최고의 방법은 새로운 싱글 페이지 애플리케이션 입니다. 이것은 개발 환경을 설정하고, 최신 JavaScript를 사용하게 해주며, 좋은 개발 경험과 프로덕션 앱 최적화를 해줍니다. 

 

실습내용

 

 

index.html 소스 확인

아래와 같이 index.html 소스를 확인할 수 있는데 여기서 

<div id="root"></div> 이 부분이 중요하다.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
    <!--
      manifest.json provides metadata used when your web app is installed on a
      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
    -->
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <title>React App</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
  </body>
</html>

 

 

App.js

App.js 에서 함수형으로 작성되어 있는 내용을 클래스형으로 수정함 (강의의 방향성을 위해)

나의 경우 아래와 같이 수정해두었다.

(강의 화면과 좀 달라서 의도한 방향대로 수정한건지 확실하진 않다. 하지만 잘돌아가는것은 확인함)

 

import logo from './logo.svg';
import { Component } from 'react';
import './App.css';

class App extends Component {
    render() {  
        return (
            <div className="App">
                <header className="App-header">
                <img src={logo} className="App-logo" alt="logo" />
                <p>
                    Hello world! Let's study React!!!
                </p>
                <a
                    className="App-link"
                    href="https://reactjs.org"
                    target="_blank"
                    rel="noopener noreferrer"
                >
                    Learn React
                </a>
                </header>
            </div>
        );
    }
}

export default App;

 

index.js 

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();

 

상기의 소스에서 css파일을 각각 import하는 것을 확인할 수 있다.

 

 

 

실행 및 빌드

cmd창에서 react-app 디렉토리로 이동하여

npm start 입력하여 실행 확인 (http://localhost:3000/index.html)

npm run build 입력하여 빌드 -> 빌드 디렉토리 생성되고 불필요한 파일과 주석 등을 제거하여 용량 축소

(http://localhost:5000 에서 확인)

 

 

 

참고를 위한 README.md

# Getting Started with Create React App

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `npm start`

Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

The page will reload if you make edits.\
You will also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `npm run build`

Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**

If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.

You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).

### Code Splitting

This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)

### Analyzing the Bundle Size

This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)

### Making a Progressive Web App

This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)

### Advanced Configuration

This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)

### Deployment

This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)

### `npm run build` fails to minify

This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)

 

 

출처: 오픈튜토리얼스 생활코딩

https://opentutorials.org/module/4058

 

개발환경 - React

수업소개 수업의 방향성과 개발환경을 마련하는 방법을 소개합니다.  강의 1 - 오리엔테이션 강의2 - React.js 개발환경의 종류 강의3 - npm을 이용해서 create-react-app 설치하기 강의4 - create-react-app을

opentutorials.org

 

반응형

댓글