Routing in Chrome Extension written in React

前端 未结 3 1641
悲&欢浪女
悲&欢浪女 2020-12-31 18:56

I want 2 pages in my Chrome extension. For example: first(default) page with list of users and second with actions for this user.

I want to display second page by cl

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-31 18:56

    While you wouldn't want to use the browser (or hash) history for your extension, you could use a memory history. A memory history replicates the browser history, but maintains its own history stack.

    import { createMemoryHistory } from 'history'
    const history = createMemoryHistory()
    

    For an extension with only two pages, using React Router is overkill. It would be simpler to maintain a value in state describing which "page" to render and use a switch or if/else statements to only render the correct page component.

    render() {
      let page = null
      switch (this.state.page) {
      case 'home':
        page = 
        break
      case 'user':
        page = 
        break
      }
      return page
    }
    

提交回复
热议问题