# 自己寫一個Router

## 自己寫一個 React Router

> 1.使用onpopstate管理瀏覽器的前後頁切換
>
> 2.換到下一頁時用`setState` 控制畫面
>
> 3.用 window\.history.pushState 推入瀏覽器 URL

```javascript
import React, { Component } from 'react';

class App extends Component {
  constructor() {
    super();
    this.state = {
      page: window.location.pathname // 重新整理時抓取URL render
    };
    window.onpopstate = () => {  // 點選瀏覽器上一頁時
      this.handleRoute(document.location.pathname, true);
    }
  }
  handleRoute(path, pop) {
    if (!pop) window.history.pushState({}, "", path);
    this.setState({ page: path });
  }
  handlelogin() {
    this.handleRoute('/mainPage');
  }
  render() {
    switch (this.state.page) {
      case '/':
      case '/login':
        return (
          <div>Login Page</div>
        )
      case '/main':
        return (
          <div>Main Page</div>
        )
      default:
        break;
    }
  }
}

export default App;
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://easonwang.gitbook.io/web_advance/react-router/zi-ji-xie-yi-ge-router.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
