最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - issue with react-router BrowserRouter in react with typescript? - Stack Overflow

programmeradmin6浏览0评论

I have gone through several similar questions but could not find something that worked. I am trying to bring in BrowserRouter from react-router but it says:

Module '"../node_modules/@types/react-router"' has no exported member 'BrowserRouter'

as per another similar question on here, I did an npm install of @types/react and @types/react-router but saved as dev dependencies. they were already in my package.json but as regular dependencies. before this, they were giving a similar issue when doing import React from 'react'. It would say that those modules could not be found, same for router. then when i did the install, those errors went away but the BrowserRouter started showing this error. I did also try 'react-router-dom'.

import React from 'react';
import { BrowserRouter, Switch, Route } from 'react-router';
import './App.css';

import Recipes from './ponents/Recipes';

const App: React.SFC = () => {

  return (
    <BrowserRouter>
      <main>
        <Switch>
          <Route exact path='/' ponent={Recipes} />
        </Switch>
      </main>
    </BrowserRouter>
  );
}

export default App;

Just expecting for the error to not be present and to be able to regularly navigate. This is my first time really using typescript.

I have gone through several similar questions but could not find something that worked. I am trying to bring in BrowserRouter from react-router but it says:

Module '"../node_modules/@types/react-router"' has no exported member 'BrowserRouter'

as per another similar question on here, I did an npm install of @types/react and @types/react-router but saved as dev dependencies. they were already in my package.json but as regular dependencies. before this, they were giving a similar issue when doing import React from 'react'. It would say that those modules could not be found, same for router. then when i did the install, those errors went away but the BrowserRouter started showing this error. I did also try 'react-router-dom'.

import React from 'react';
import { BrowserRouter, Switch, Route } from 'react-router';
import './App.css';

import Recipes from './ponents/Recipes';

const App: React.SFC = () => {

  return (
    <BrowserRouter>
      <main>
        <Switch>
          <Route exact path='/' ponent={Recipes} />
        </Switch>
      </main>
    </BrowserRouter>
  );
}

export default App;

Just expecting for the error to not be present and to be able to regularly navigate. This is my first time really using typescript.

Share asked Sep 11, 2019 at 13:59 FizloFizlo 751 silver badge7 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

According to the documentation of React Router, BrowserRouter gets imported from 'react-router-dom', not 'react-router'.

Here is the example code given in the link above.

import { BrowserRouter } from 'react-router-dom'

<BrowserRouter
  basename={optionalString}
  forceRefresh={optionalBool}
  getUserConfirmation={optionalFunc}
  keyLength={optionalNumber}
>
  <App/>
</BrowserRouter>

Here is a SO question about the difference between react-router and react-router-dom and the accepted answer contains the following.

react-router contains all the mon ponents between react-router-dom and react-router-native. When should you use one over the other? If you're on the web then react-router-dom should have everything you need as it also exports the mon ponents you'll need. If you're using React Native, react-router-native should have everything you need for the same reason. So you'll probably never have to import anything directly from react-router.

I did also try 'react-router-dom'.

It should work, e.g. this code

import { BrowserRouter, Route, Switch } from "react-router-dom";

works.

Both react-dom and react-router-dom packages should be installed. That means both can be found in the dependencies section of package.json file. Working sample project using Typescript can be found here.

发布评论

评论列表(0)

  1. 暂无评论