反应require(“ history”)。createBrowserHistory`而不是`require(“ history / createBrowserHistory”)

时间:2019-04-02 04:04:46

标签: reactjs react-redux react-router history.js

所以基本上我在使用历史库进行反应时遇到问题。

由于最新版本是我应该尝试降级历史版本,还是因为错误指出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.

AND

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;

显示错误时,不会呈现任何内容。

9 个答案:

答案 0 :(得分:6)

使用大括号导入pageIndex。它已导出为named export

creatBrowserHistory

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

// history.js

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

答案 1 :(得分:3)

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

对此 import { createBrowserHistory } from 'history'

答案 2 :(得分:1)

在我的代码中,运行单元测试时发生此错误。通过不同地解释ES6代码,可以产生酶或笑话。将包历史记录导出设置为默认值。

我现在的导入代码

import { createBrowserHistory } from 'history'

这是完整的history.js代码

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

答案 3 :(得分:0)

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

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

从“为历史创建的文件路径将起作用”导入历史

答案 4 :(得分:0)

goto node_modules> dva> lib> index.js enter image description here

index.js enter image description here

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

答案 5 :(得分:0)

使用该建议,我可以消除渲染控制台中的错误。

// 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

答案 6 :(得分:0)

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

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

答案 7 :(得分:0)

更新 React-Router-Dom

如果您使用纱线

写 => yarn add React-Router-Dom

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

Like this

答案 8 :(得分:0)

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