简体   繁体   中英

React Router/History Can't Resolve "history/createBrowserHistory"

I cloned my react project today and the project was working fine on my other mac. Then I started to face some problems with my router, especially the history. The code in my history.ts is:

import createHistory from 'history/createBrowserHistory';

export default createHistory();

My app.tsx is:

import React from 'react';
import { Route, Router, Switch, } from 'react-router-dom';
import { connect } from 'react-redux';

import history from '../history';

const App = (props: any) => {
    return (
        <div>
            <Router history={history}>
                <div>
                    <Header/>
                    <Route path="/" exact component={Home}/>
                    <Route path="/about" exact component={About} />
                    <Route path="/media" exact component={Media}/>
                    <Switch>
                        <Route path="/login" exact component={Login}/>
                        <PrivateRoute path="/menu" exact component={Menu} auth={props.isSignedIn} />
                        <PrivateRoute path="/create" exact component={PostCreate} auth={props.isSignedIn} />
                        <PrivateRoute path="/delete/:id" exact component={PostDelete} auth={props.isSignedIn}/>
                    </Switch>
                </div>
            </Router>
        </div>
    );
};

I tried to change my history.ts to:

import { createBrowserHistory } from "history";
export default createBrowserHistory();

and no success, I also tried to change to

import { createBrowserHistory, History } from "history";
const history: History = createBrowserHistory();
export default history

And no success too, it's strange because my application was working fine months ago. I've searched all over Stack Overflow and no solution seems to fit. Any help will be appreciated.

I've solved, I noticed that the createBrowserHistory.d.ts weren't getting created. Probably because of my fault yarn install . I removed the project and re-cloned and installed the libraries using npm Then worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM