React와 Spring으로 게시판 만들기

[React] Route로 페이지 이동하기 (5) - 글쓰기 버튼 클릭 시 페이지 이동

Evolving Developer 2023. 3. 4. 16:29

목표

 + 버튼을 누르면 글쓰기 페이지로 이동하고 싶음

 - Route 사용

 - <Link to=""></Link> 사용

 - http://localhost:3000/write (글쓰기 페이지 주소)

 

 

Route 라이브러리 설치

- React 터미널 열기

npm install 'react-router-dom'

App.js

- 'react-router-dom' 라이브러리 임포트

- <Route path='{http://localhost:3000 이후 주소}' component={import 한 리액트 파일 이름}> 로 라우팅

import { BrowserRouter as Routes, Route, Switch } from 'react-router-dom';
import Write from './BoardList/Write.js';
import BoardList from './BoardList/BoardList.js';

function App() {
    return (
        <div>
            <Routes>
            	<Route path='/write' component={Write} />
                <Route path='/' component={BoardList} />
            </Routes>
        </div>
    );
}

export default App;

- http://localhost:3000 으로 접근 -> <BoardList/> 로 매핑

- http://localhost:3000/write 로 접근 -> <Write/> 로 매핑

- <Routes> 말고 <Router>로 할 경우 아래 에러 발생 (버전 차이)

Route 에러

 

 

Uncaught Error: A <Route> is only ever to be used as the child of <Routes> element, never rendered directly. Please wrap your <Route> in a <Routes>

 

번역

잡히지 않은 오류: <Route>는 <Routes> 요소의 자식으로만 사용되며 직접 렌더링되지 않습니다. <Route>를 <Routes>로 감싸주세요.

 

 

 

에러 참고

 

A <Route> is only ever to be used as the child of <Routes> element, never rendered directly. Please wrap your <Route> in a <Rout

이번 글에서는 A is only ever to be used as the child of element, never rendered directly. Please wrap your in a . 에러 해결 방법에 대하여 알아보겠습니다. A is only ever to be used as the child of element, never rendered directly. Ple

itprogramming119.tistory.com

결과

 

- <Write>와 <BoardList> 둘 다 실행되는 오류 발생

=> <Switch> 태그로 <Route> 감싸기

 

 

 

 

App.js 수정

import { BrowserRouter as Routes, Route, Switch } from 'react-router-dom';
import Write from './BoardList/Write.js';
import BoardList from './BoardList/BoardList.js';

function App() {
    return (
        <div>
            <Routes>
            	<Switch>
            		<Route path='/write' component={Write} />
                	<Route path='/' component={BoardList} />
                </Switch>
            </Routes>
        </div>
    );
}

export default App;

주의사항

- 아래처럼 <Route>의 순서를 바꾸지 않기 

- 'http://localhost:3000/' 뒤에 어떤 주소가 들어오더라도 BoardList로 매핑되어 개발자 의도대로 움직이지 않기 때문

=> 세부 주소일수록 위에 두자.

<Route path='/' component={BoardList} />
<Route path='/write' component={Write} />

WriteButton.js

import './WriteButton.css';
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faPlus } from "@fortawesome/free-solid-svg-icons";
import { Link } from 'react-router-dom';

function WriteButton() {

  return (
    <div>
        <button className='write__button'>
            <Link to="/write">
            <i><FontAwesomeIcon icon={faPlus} size="4x"/></i>
            </Link>
        </button>
    </div>
  );
}

export default WriteButton;

- <button> 태그 내부에 <Link> 태그 삽입

- 버튼을 누르면 'http://localhost:3000/write' 페이지로 이동

결과

참고

 

[React] React-Router를 활용해 원하는 페이지로 이동하기

React-Router 리액트 라우터는 여러 페이지들을 연결시켜주는 역할을 하는 친구입니다. 리액트 자체에서 지원해주는 기술은 아니지만 리액트에서 라우팅을 한다면 이 서드파티를 사용하는 것이 국

junjangsee.tistory.com

 

 

[리액트] 페이지 이동(Routes)

리액트를 처음 사용했을 때, 페이지 이동을 a태그를 사용하여 href를 통해 페이지를 이동했었습니다. 하지만 해당 방식을 사용하면 리액트를 사용할 필요가 없습니다. 리액트를 사용하면서 페이

stickode.tistory.com