2

I've read that ReactRouter sucks.

How can I render different components strictly based on particular routes, i.e, with:

ReactDOM.render(<Game />, document.getElementById("root"));

How could I change this to use something like this that I found here:

const routes = [
  { path: '/', action: () => <HomePage /> },
  { path: '/game', action: () => <Game /> },
  { path: '/game2', action: () => <Game2 /> }
];

I feel like this should NOT be that complicated.

1 Answer 1

1

Here is what I have done for a simple app I have:

class SimpleRouter extends React.Component {
  render() {
    {this.getContainer()}
  }

  getContainer() {
    const url = window.location.href;

    const routes = [
      { path: '/', action: () => <HomePage /> },
      { path: '/game', action: () => <Game /> },
      { path: '/game2', action: () => <Game2 /> }
    ];

    const route = routes.find(r => url.endsWith(r));

    if (route && route.action) {
      return route.action();
    } else {
      return <DefaultContainer />;
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

It doesn't work for subpaths like a/b (not found). Is there a workaround as long as I don't want to use React Router?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.