首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React (“history”).createBrowserHistory`而不是‘require(“history/createBrowserHistory”)

React (“history”).createBrowserHistory`而不是‘require(“history/createBrowserHistory”)
EN

Stack Overflow用户
提问于 2019-04-02 04:04:45
回答 12查看 61.8K关注 0票数 38

因此,基本上,我在使用历史库的反应上有一个问题。

是不是因为最近的版本,我应该试着降低历史版本的等级,但是由于错误声明Support for the latter will be removed in the next major release.,那么我应该如何更改,以及应该在哪里更改呢?

上面写着:

代码语言:javascript
复制
Warning: Please use `require("history").createBrowserHistory` instead of `require("history/createBrowserHistory")`. Support for the latter will be removed in the next major release.

代码语言:javascript
复制
Warning: Please use `require("history").createHashHistory` instead of `require("history/createHashHistory")`. Support for the latter will be removed in the next major release.

我不太知道怎么修理它。

代码语言:javascript
复制
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 }
}
代码语言:javascript
复制
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

代码语言:javascript
复制
import createHistory from 'history/createBrowserHistory';
export default createHistory;

当它显示错误时,就不会呈现任何错误。

EN

回答 12

Stack Overflow用户

回答已采纳

发布于 2019-04-12 14:38:09

导入带花括号的creatBrowserHistory。它作为一个命名出口导出。

代码语言:javascript
复制
// history.js

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

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

代码语言:javascript
复制
// 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>
);
票数 53
EN

Stack Overflow用户

发布于 2019-07-30 18:41:56

我改变了这个

import createHistory from 'history/createBrowserHistory'

对于这个import { createBrowserHistory } from 'history'

票数 17
EN

Stack Overflow用户

发布于 2019-05-13 08:04:02

在我的代码中,运行单元测试时会发生此错误。通过对ES6代码的不同解释,酶或玩笑是可能的。使在包历史导出默认。

我现在的进口代码

代码语言:javascript
复制
import { createBrowserHistory } from 'history'

这是完整的history.js代码

代码语言:javascript
复制
import { createBrowserHistory } from 'history';
export default createBrowserHistory(); 
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55466802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档