@alexseitsinger/previous-pathname-middleware

Adds the previous pathname to router in redux store.

Usage no npm install needed!

<script type="module">
  import alexseitsingerPreviousPathnameMiddleware from 'https://cdn.skypack.dev/@alexseitsinger/previous-pathname-middleware';
</script>

README

Previous Pathname Middleware

Description

Adds the previous pathname to router in redux store.

Usage

In store:

import { createStore, compose, applyMiddleware } from "redux"
import { routerMiddleware } from "connected-react-router"
import previousPathnameMiddleware from "@alexseitsinger/previous-pathname-middleware"

import createRootReducer from "../reducer"

function configureStore(history, initialState = {}) {
    const rootReducer = createRootReducer(history)
    const middleware = [previousPathnameMiddleware, routerMiddleware(history)]
    const storeEnhancers = compose(applyMiddleware(...middleware))
    const store = createStore(rootReducer, initialState, storeEnhancers)
    return store
}

In component:

import React from "react"
import PropTypes from "prop-types"
import { connect } from "react-redux"

function App({ previousPathname }) {
    return <div>App</div>
}

App.propTypes = {
    previousPathname: PropTypes.string.isRequired
}

const mapStateToProps = (state) => {
    return {
        previousPathname: state.router.previousPathname
    }
}

export default connect(mapStateToProps)(App)