gpt4 book ai didi

reactjs - React 需要 ("history").createBrowserHistory` 而不是 `require("历史记录/createBrowserHistory")

转载 作者:行者123 更新时间:2023-12-03 13:10:28 26 4
gpt4 key购买 nike

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

是否因为最新版本,我应该尝试降级历史版本,但由于错误指出对后者的支持将在下一个主要版本中删除。那么我应该如何更改和我应该在哪里更改它?

它说:

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;

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

最佳答案

使用大括号导入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>
);

关于reactjs - React 需要 ("history").createBrowserHistory` 而不是 `require("历史记录/createBrowserHistory"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55466802/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com