React require("history").createBrowserHistory` 而不是 `require("history/createBrowserHistory")

React require("history").createBrowserHistory` instead of `require("history/createBrowserHistory")

所以基本上我在使用 react 中的历史库时遇到了问题。

是不是因为是最新版本,我应该尝试降级历史版本但是因为错误指出Support for the latter will be removed in the next major release.所以我应该如何更改以及我应该在哪里更改它?

它说:

Warning: Please use `require("history").createBrowserHistory` instead of `require("history/createBrowserHistory")`. Support for the latter will be removed in the next major release.

Warning: Please use `require("history").createHashHistory` instead of `require("history/createHashHistory")`. Support for the latter will be removed in the next major release.

我不太确定如何修复它。

import createHistory from './history'

import { applyMiddleware, compose, createStore } from 'redux'
import { routerMiddleware } from 'connected-react-router'
import { persistStore, persistReducer } from 'redux-persist'
import storage from 'redux-persist/es/storage'
import thunk from 'redux-thunk'
import createRootReducer from './reducers'

export const history = createHistory();

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const persistConfig = {
  key: 'root',
  storage
};

const reducers = persistReducer( persistConfig, createRootReducer(history));
const exampleMiddleware =  store => next => action => {
  // if (action.type === 'message'){
  //   do something
  // } else {
  //   next(action);
  // }
}

export default () => {
  const store = createStore(
    reducers,
    composeEnhancers(applyMiddleware(routerMiddleware(history), thunk, exampleMiddleware))
  );
  let persistor = persistStore(store)

  return  { store, persistor }
}
import React, { Component } from 'react';
import { Provider, ReactReduxContext } from 'react-redux';
// import { createStore } from 'redux';
import { ConnectedRouter } from 'connected-react-router'
import { PersistGate } from 'redux-persist/integration/react'

import configureStore, { history } from './configureStore'
import Routers from './Routers';

const { persistor, store } = configureStore();

class App extends Component {
  render() {
    return (

      <Provider store={store} context={ReactReduxContext}>
        <div> SYNTIFY </div>
        <PersistGate loading={null} persistor={persistor}>

          <ConnectedRouter history={history} context={ReactReduxContext}>
            <Routers />
          </ConnectedRouter>

        </PersistGate>
      </Provider>

    );
  }
}

export default App;

history.js

import createHistory from 'history/createBrowserHistory';
export default createHistory;

因为它显示错误,所以没有渲染任何内容。

只需为历史创建新文件并添加

    import createHistory from 'history/createBrowserHistory';
    export default createHistory();

从 'the file path created for history it will work'

导入历史记录

用大括号导入 creatBrowserHistory。它导出为 named export.

// history.js

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

然后导入并在索引中使用。

// index.js
import history from "./history";
import { Provider } from "react-redux";
import store from "./store/store";

const AppContainer = () => (
    <Router history={history}>
        <Provider store={store}>
             <Route path="/" component={App} />
        </Provider>
    </Router>
);

在我的代码中,这个错误发生在 运行 单元测试时。通过以不同方式解释 ES6 代码,可以产生酶或笑话。使包历史导出默认。

现在我的导入代码

import { createBrowserHistory } from 'history'

这里是完整的history.js代码

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

我已经更改了这个
import createHistory from 'history/createBrowserHistory'

到这个 import { createBrowserHistory } from 'history'

转到 node_modules > dva > lib > index.js

index.js

来源:https://www.cnblogs.com/fairylee/p/11198868.html

使用建议,我能够在渲染时消除控制台中的错误。

// NO IMPORT ABOVE  (just set the import directly to a variable)
    const history = require("history").createBrowserHistory()


// then you can 
if( *some-requirement*){
history.push("/desiredRoute")
}. 
// right from your App.js

这个导入对我有用 var createHistory = require('history').createBrowserHistory; 而不是这个导入 import createHistory from 'history/createBrowserHistory'; 历史文件如下:

var createHistory = require('history').createBrowserHistory;
export default createHistory();

更新 React-Router-Dom

如果您使用的是 yarn

写入 => yarn add React-Router-Dom

会自动更新到当前版本的路由库

Like this

试试这个:安装这个版本的 react-router-dom@5.2.0。如果有错误,安装webpack@3.1.0.

试试这个

  • 转到node_modules/history/createBrowserHistory并按照警告说的做

  • 删除此 ('createBrowserHistory') 在新行中用此替换它:require("history").createBrowserHistory

然后

  • 转到node_modules/history/createHashHistory

  • 删除 ('createHashHistory') 在新行中将其替换为:require("history").createHashHistory

您需要 API 迁移,

// createBrowserHistory  was createHistory in createBrowserHistory
import { createBrowserHistory as history} from 'history'

import { Provider } from "react-redux";
import store from "./store/store";

const AppContainer = () => (
    <Router history={history}>
        <Provider store={store}>
             <Route path="/" component={App} />
        </Provider>
    </Router>
);

感谢@CrsCaballero 的