Giter Club home page Giter Club logo

react_router's Introduction

PIP LIST

$ yarn add react-router-dom
$ yarn add cross-env --dev

cross-env : 프로젝트에서 NODE_PATH 를 사용하여 절대경로로 파일을 불러오기 위하여 환경 변수를 설정 할 때 운영체제마다 방식이 다르므로 공통적인 방법으로 설정 할 수 있게 해주는 라이브러리


디렉토리

  • src/components: 컴포넌트들이 위치하는 디렉토리
  • src/pages: 각 라우트들이 위치하는 디렉토리
  • src/client: 브라우저 측에서 사용할 최상위 컴포넌트
    추후 서버사이드 렌더링을 위해 별개의 디렉토리 생성
    (서버사이드 렌더링을 할 때에는 서버 전용 라우터를 써야함)
    여기서 라우터를 설정
  • src/server: 서버측에서 사용 할 리액트 관련 코드 저장
  • src/shared: 서버와 클라이언트에서 공용으로 사용되는 컴포넌트 App.js 위치
  • src/lib: 나중에 웹 연동을 구현 할 때 사용 할 API와 코드스플리팅 할 때 필요한 코드가 여기에 위치

    NODE_ENV 설정

    "scripts": {
        "start": "cross-env NODE_PATH=src react-scripts start",
        "build": "cross-env NODE_PATH=src react-scripts build",
        "test": "react-scripts test --env=jsdom",
        "eject": "react-scripts eject"
    }
    
  • package.json의 scripts부분을 이렇게 설정함으로써 ‘../components/Something’ 이런식으로 불러와야 하는 코드를 ‘components/Something’으로 간편하게 불러올 수 있음

    라우트 파라미터

    params 사용
    라우트로 설정한 컴포넌트는 3가지의 props를 전달 받는다.

  • history : push, replace를 통해 앞 뒤 경로로 이동가능
  • location : 이 객체는 현재 경로에 대한 정보를 지니고 있고 URL 쿼리 (/about?foo=bar 형식) 정보를 가지고 있음
  • match : 어떤 라우트와 매칭되었는지에 대한 정보를 가지고 있고 params (/about/:name 형식)정보를 가지고 있음

    'url 쿼리'를 다루기 위한 라이브러리

    $ yarn add query-string
    

    라우트 이동하기

    <a href...>foo</a>를 이용하면 새로고침이 되므로 권장하지 않음 => link컴포넌트를 사용한다.

  • Menu.js 컴포넌트를 만들어서 루트 앱에 적용시키면된다.
    //Menu.js
    import React from 'react';
    import { Link } from 'react-router-dom';
    
    const Menu = () => {
        return (
            <div>
                <ul>
                    <li><Link to="/">Home</Link></li>
                    <li><Link to="/about">About</Link></li>
                    <li><Link to="/about/foo">About Foo</Link></li>
                </ul>
                <hr/>
            </div>
        );
    };
    
    export default Menu;
    //style을 주고싶으면 Navelink를 쓴다.
    //활성화 되었을 시 스타일 : activeStyle , class지정은 activeClassName
    import React from 'react';
    import { NavLink } from 'react-router-dom';
    
    const Menu = () => {
        const activeStyle = {
            color: 'green',
            fontSize: '2rem'
        };
    
        return (
            <div>
                <ul>
                    <li><NavLink exact to="/" activeStyle={activeStyle}>Home</NavLink></li>
                    <li><NavLink exact to="/about" activeStyle={activeStyle}>About</NavLink></li>
                    <li><NavLink to="/about/foo" activeStyle={activeStyle}>About Foo</NavLink></li>
                </ul>
                <hr/>
            </div>
        );
    };
    
    export default Menu;
  • react_router's People

    Contributors

    tedhoon avatar

    Watchers

    James Cloos avatar

    Recommend Projects

    • React photo React

      A declarative, efficient, and flexible JavaScript library for building user interfaces.

    • Vue.js photo Vue.js

      🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

    • Typescript photo Typescript

      TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

    • TensorFlow photo TensorFlow

      An Open Source Machine Learning Framework for Everyone

    • Django photo Django

      The Web framework for perfectionists with deadlines.

    • D3 photo D3

      Bring data to life with SVG, Canvas and HTML. 📊📈🎉

    Recommend Topics

    • javascript

      JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

    • web

      Some thing interesting about web. New door for the world.

    • server

      A server is a program made to process requests and deliver data to clients.

    • Machine learning

      Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

    • Game

      Some thing interesting about game, make everyone happy.

    Recommend Org

    • Facebook photo Facebook

      We are working to build community through open source technology. NB: members must have two-factor auth.

    • Microsoft photo Microsoft

      Open source projects and samples from Microsoft.

    • Google photo Google

      Google ❤️ Open Source for everyone.

    • D3 photo D3

      Data-Driven Documents codes.