GitXplorerGitXplorer
T

react-router-global-history

public
3 stars
0 forks
19 issues

Commits

List of commits on branch master.
Unverified
70e0d107f09596b9b70fe4a0e7c18332fbbbacbc

Releasing version 1.1.0

TTomekmularczyk committed 5 years ago
Unverified
8b5486363793e0c5e8d5b5331396c4bafa6f5dd4

add release script

TTomekmularczyk committed 5 years ago
Unverified
d86f4deb0dbbe3692ba6165b935be8ee2590a8d8

add release script

TTomekmularczyk committed 5 years ago
Unverified
577794a0c78043a8efb8a0a8b3996ec1b4961ce0

add typings

TTomekmularczyk committed 5 years ago
Unverified
9b0df70341d4a055284d4c39c855bf577c6c0456

update packages

TTomekmularczyk committed 5 years ago
Verified
c6d6ef7ac2a770db612bd43096abe1439823462b

Update README.md

TTomekmularczyk committed 6 years ago

README

The README file for this repository.

react-router-global-history npm version

This is a simple helper library to live alongside react-router-4 to help you access history object from the wherever file in your application (like in redux actions).

You simply need to mount component on top of your application like:

import { BrowserRouter } from 'react-router-dom';
import { ReactRouterGlobalHistory } from 'react-router-global-history';

function render() {
  ReactDOM.render(
    <BrowserRouter>
        <div>
            <ReactRouterGlobalHistory />
            //.....
        </div>
    </BrowserRouter>
    document.getElementById('app'),
  );
}

This will initialize internal history variable (in constructor) with the one provided by withRouter HOC. Which means you can import it wherever in the app like:

import getHistory from 'react-router-global-history'; 

export const goToPage = () => (dispatch) => {
  dispatch({ type: GO_TO_SUCCESS_PAGE });
  getHistory().push('/success');
};

That's it! But there's one catch. You need to call getHistory() after component has been mounted. For example initialization of redux happens before app renders so if you try to access history like: reducer.js:

import getHistory from 'react-router-global-history'; 

const history = getHistory(); // error!

... you will get an error. So make sure you call it when the ReactRouterGlobalHistory component has already been mounted!