Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routing in Chrome Extension written in React

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 clicking on user(ClickableListItem in my case). I use React and React Router. Here the component in which I have:

class Resents extends React.Component {
constructor(props) {
  super(props);
  this.handleOnClick = this.handleOnClick.bind(this);
}
handleOnClick() {
  console.log('navigate to next page');
  const path = '/description-view';
  browserHistory.push(path);
}
render() {
  const ClickableListItem = clickableEnhance(ListItem);
  return (
    <div>
      <List>
        <ClickableListItem
          primaryText="Donald Trump"
          leftAvatar={<Avatar src="img/some-guy.jpg" />}
          rightIcon={<ImageNavigateNext />}
          onClick={this.handleOnClick}
        />
      // some code missed for simplicity
      </List>
    </div>
  );
}
}

I also tried to wrap ClickableListItem into Link component(from react-router) but it does nothing.

Maybe the thing is that Chrome Extensions haven`t their browserHistory... But I don`t see any errors in console...

What can I do for routing with React?

like image 390
Maxim Romanyuk Avatar asked Dec 26 '16 19:12

Maxim Romanyuk


People also ask

Can you write a Chrome extension in React?

Normally, when you create a Chrome extension, you need to create a manifest. json file, but we're in luck! Create React App creates a manifest. json file by default — you can find it in your public folder.

Can React do routing?

React Router is a standard library for routing in React. It enables the navigation among views of various components in a React Application, allows changing the browser URL, and keeps the UI in sync with the URL.

How do I enable routing in React?

To enable routing in our React app, we first need to import BrowserRouter from react-router-dom . This should hold everything in our app where routing is needed. That means, if we need routing in our entire app, we must wrap our higher component with BrowserRouter .


1 Answers

I know this post is old. Nevertheless, I'll leave my answer here just in case somebody still looking for it and want a quick answer to fix their existing router.

In my case, I get away with just switching from BrowserRouter to MemoryRouter. It works like charm without a need of additional memory package!

import { MemoryRouter as Router } from 'react-router-dom';

ReactDOM.render(
    <React.StrictMode>
        <Router>
            <OptionsComponent />
        </Router>
    </React.StrictMode>,
    document.querySelector('#root')
);

You can try other methods, that suits for you in the ReactRouter Documentation

like image 115
SnekNOTSnake Avatar answered Sep 18 '22 13:09

SnekNOTSnake