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

12

39

So basically i am having a problem in using the history library in react.

Is it because of the latest version should i try to downgrade the history version but as the error states that Support for the latter will be removed in the next major release. so how should i change and where should i change it?

it says:

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.

I am not quite sure how to fix it.

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;

As it showing error nothing gets rendered.

Brister answered 2/4, 2019 at 4:4 Comment(0)
R
55

Import creatBrowserHistory with curly brackets. It's exported as a named export.

// history.js

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

Then import and use it in index.

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

Richella answered 12/4, 2019 at 14:38 Comment(3)
Just a note, mostly it is the issue as mentioned by @Richella but rarely as it happened in my case, I had passed the component incorrectly in the <Route component={<div>hi</div>} /> (imported from react-router-dom). Removal of that resolved the issue for me.Alodi
Why did you define Router before the redux store Provider?Alvarez
@Alvarez no particular reason. I don't believe the order matters in this case.Richella
B
17

I've changed this
import createHistory from 'history/createBrowserHistory'

to this import { createBrowserHistory } from 'history'

Bugloss answered 30/7, 2019 at 18:41 Comment(0)
S
3

In my code, this error occurs when running a unit test. An enzyme or jest is possible by interpreting the ES6 code differently. Make in the package history export default.

My import code now

import { createBrowserHistory } from 'history'

Here is the full history.js code

import { createBrowserHistory } from 'history';
export default createBrowserHistory(); 
Senary answered 13/5, 2019 at 8:4 Comment(0)
G
2

Having the solution approved above wasn't enough for me:

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

I solved this issue updating react-router and react-router-dom to:

"react-router": "^5.3.3",
"react-router-dom": "^5.3.3"
Godinez answered 30/8, 2022 at 10:1 Comment(1)
Mine was the same, the above fix alone wasn't enough. Mine (for node 16) required: "react-router": "4.3.1", "react-router-dom": "4.3.1",Revanchism
M
1

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

index.js enter image description here

source from: https://www.cnblogs.com/fairylee/p/11198868.html

Munmro answered 5/8, 2019 at 11:24 Comment(0)
T
1

Using the suggestion I was able to get rid of the error in the console on render.

// 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
Tactful answered 1/9, 2020 at 4:26 Comment(0)
E
1

Try this: Install this version of [email protected]. If there is an error, install [email protected].

Edgardoedge answered 5/5, 2021 at 21:59 Comment(0)
R
0

just create new file for history and add

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

import history from 'the file path created for history it will work'

Ringent answered 2/4, 2019 at 4:44 Comment(7)
Can i just directly import it and use it? updted my answer above pls lookBrister
yes you can direclty import it in App component and attach it to ConnectedRouterRingent
@kishanJiswal Can you see the code above? i updated my configStore.js and it still shows the same errorBrister
i will suggest to keep separate file for history and import and useRingent
@kishanJiswal look at the edit code above. is that what you meant? It didnt workBrister
Let us continue this discussion in chat.Brister
Did you find a solution to this? I am facing .a similar issueIroquois
S
0

This import worked for me var createHistory = require('history').createBrowserHistory; instead of this import import createHistory from 'history/createBrowserHistory'; The history file is given below:

var createHistory = require('history').createBrowserHistory;
export default createHistory();
Segarra answered 14/12, 2020 at 2:10 Comment(0)
G
0

Update the React-Router-Dom library

If you're using yarn

write => yarn add React-Router-Dom

it will automatically update to the current version of Router library

Like this

Gattis answered 7/1, 2021 at 6:8 Comment(0)
D
0

try this

  • go to node_modules/history/createBrowserHistory and do as the warning says

  • delete this ('createBrowserHistory') replace it with this in new line: require("history").createBrowserHistory

then

  • go to node_modules/history/createHashHistory

  • delete ('createHashHistory') replace it with this in new line: require("history").createHashHistory

Dicks answered 8/1, 2022 at 10:55 Comment(0)
S
0

you need an API migration,

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

Thx to @CrsCaballero's

Sayers answered 13/4, 2022 at 17:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.