Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 344
Nick Bb Avatar asked Apr 02 '19 04:04

Nick Bb


People also ask

Does react Router use history API?

React Router uses the history package, which builds on the browser history API to provide an interface to which we can use easily in React apps. location - (object) The current location. May have the following properties: pathname - (string) The path of the URL.

What is the difference between BrowserRouter and Router?

The main difference between the two is the way they store the URL and communicate with your web server. A <BrowserRouter> uses regular URL paths.


3 Answers

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> );  
like image 58
Giwan Avatar answered Sep 20 '22 11:09

Giwan


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

to this import { createBrowserHistory } from 'history'

like image 25
CrsCaballero Avatar answered Sep 20 '22 11:09

CrsCaballero


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(); 
like image 35
Sunny Sultan Avatar answered Sep 21 '22 11:09

Sunny Sultan