基本上,我在使用React中的history库时遇到了问题。
这是否是因为最新版本的问题?我应该尝试降级history版本吗?但是错误信息显示“支持后者将在下一个主要版本中被删除”,那么我该如何更改,以及在哪里更改呢?
错误信息如下:
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;
历史记录.js
import createHistory from 'history/createBrowserHistory';
export default createHistory;
由于显示错误,没有任何内容被渲染出来。
Provider
之前定义了Router
? - vsync