react-router-last-location
Advanced tools
Comparing version 1.1.0 to 2.0.0-beta.0
@@ -1,215 +0,1 @@ | ||
module.exports = | ||
/******/ (function(modules) { // webpackBootstrap | ||
/******/ // The module cache | ||
/******/ var installedModules = {}; | ||
/******/ | ||
/******/ // The require function | ||
/******/ function __webpack_require__(moduleId) { | ||
/******/ | ||
/******/ // Check if module is in cache | ||
/******/ if(installedModules[moduleId]) { | ||
/******/ return installedModules[moduleId].exports; | ||
/******/ } | ||
/******/ // Create a new module (and put it into the cache) | ||
/******/ var module = installedModules[moduleId] = { | ||
/******/ i: moduleId, | ||
/******/ l: false, | ||
/******/ exports: {} | ||
/******/ }; | ||
/******/ | ||
/******/ // Execute the module function | ||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); | ||
/******/ | ||
/******/ // Flag the module as loaded | ||
/******/ module.l = true; | ||
/******/ | ||
/******/ // Return the exports of the module | ||
/******/ return module.exports; | ||
/******/ } | ||
/******/ | ||
/******/ | ||
/******/ // expose the modules object (__webpack_modules__) | ||
/******/ __webpack_require__.m = modules; | ||
/******/ | ||
/******/ // expose the module cache | ||
/******/ __webpack_require__.c = installedModules; | ||
/******/ | ||
/******/ // define getter function for harmony exports | ||
/******/ __webpack_require__.d = function(exports, name, getter) { | ||
/******/ if(!__webpack_require__.o(exports, name)) { | ||
/******/ Object.defineProperty(exports, name, { | ||
/******/ configurable: false, | ||
/******/ enumerable: true, | ||
/******/ get: getter | ||
/******/ }); | ||
/******/ } | ||
/******/ }; | ||
/******/ | ||
/******/ // getDefaultExport function for compatibility with non-harmony modules | ||
/******/ __webpack_require__.n = function(module) { | ||
/******/ var getter = module && module.__esModule ? | ||
/******/ function getDefault() { return module['default']; } : | ||
/******/ function getModuleExports() { return module; }; | ||
/******/ __webpack_require__.d(getter, 'a', getter); | ||
/******/ return getter; | ||
/******/ }; | ||
/******/ | ||
/******/ // Object.prototype.hasOwnProperty.call | ||
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; | ||
/******/ | ||
/******/ // __webpack_public_path__ | ||
/******/ __webpack_require__.p = ""; | ||
/******/ | ||
/******/ // Load entry module and return exports | ||
/******/ return __webpack_require__(__webpack_require__.s = 3); | ||
/******/ }) | ||
/************************************************************************/ | ||
/******/ ([ | ||
/* 0 */ | ||
/***/ (function(module, exports) { | ||
module.exports = require("react"); | ||
/***/ }), | ||
/* 1 */ | ||
/***/ (function(module, exports) { | ||
module.exports = require("react-router-dom"); | ||
/***/ }), | ||
/* 2 */ | ||
/***/ (function(module, __webpack_exports__, __webpack_require__) { | ||
"use strict"; | ||
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "b", function() { return getLastLocation; }); | ||
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0_react__ = __webpack_require__(0); | ||
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0_react___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0_react__); | ||
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1_prop_types__ = __webpack_require__(5); | ||
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1_prop_types___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_1_prop_types__); | ||
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_2_react_router_dom__ = __webpack_require__(1); | ||
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_2_react_router_dom___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_2_react_router_dom__); | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
var lastLocation = null; | ||
var LastLocationProvider = function (_Component) { | ||
_inherits(LastLocationProvider, _Component); | ||
function LastLocationProvider() { | ||
_classCallCheck(this, LastLocationProvider); | ||
return _possibleConstructorReturn(this, (LastLocationProvider.__proto__ || Object.getPrototypeOf(LastLocationProvider)).apply(this, arguments)); | ||
} | ||
_createClass(LastLocationProvider, [{ | ||
key: 'componentWillReceiveProps', | ||
value: function componentWillReceiveProps(nextProps) { | ||
if (this.props.location === nextProps.location) { | ||
return; | ||
} | ||
if (!this.props.watchOnlyPathname) { | ||
lastLocation = _extends({}, this.props.location); | ||
return; | ||
} | ||
if (this.props.location.pathname === nextProps.location.pathname) { | ||
return; | ||
} | ||
lastLocation = _extends({}, this.props.location); | ||
} | ||
}, { | ||
key: 'render', | ||
value: function render() { | ||
return this.props.children; | ||
} | ||
}]); | ||
return LastLocationProvider; | ||
}(__WEBPACK_IMPORTED_MODULE_0_react__["Component"]); | ||
LastLocationProvider.propTypes = { | ||
watchOnlyPathname: __WEBPACK_IMPORTED_MODULE_1_prop_types___default.a.bool, | ||
location: __WEBPACK_IMPORTED_MODULE_1_prop_types___default.a.shape({ | ||
pathname: __WEBPACK_IMPORTED_MODULE_1_prop_types___default.a.string, | ||
search: __WEBPACK_IMPORTED_MODULE_1_prop_types___default.a.string, | ||
hash: __WEBPACK_IMPORTED_MODULE_1_prop_types___default.a.string, | ||
key: __WEBPACK_IMPORTED_MODULE_1_prop_types___default.a.string, | ||
state: __WEBPACK_IMPORTED_MODULE_1_prop_types___default.a.object | ||
}).isRequired, | ||
children: __WEBPACK_IMPORTED_MODULE_1_prop_types___default.a.node.isRequired | ||
}; | ||
LastLocationProvider.defaultProps = { | ||
watchOnlyPathname: false | ||
}; | ||
var getLastLocation = function getLastLocation() { | ||
return lastLocation; | ||
}; | ||
/* harmony default export */ __webpack_exports__["a"] = (Object(__WEBPACK_IMPORTED_MODULE_2_react_router_dom__["withRouter"])(LastLocationProvider)); | ||
/***/ }), | ||
/* 3 */ | ||
/***/ (function(module, __webpack_exports__, __webpack_require__) { | ||
"use strict"; | ||
Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); | ||
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__withLastLocation__ = __webpack_require__(4); | ||
/* harmony reexport (binding) */ __webpack_require__.d(__webpack_exports__, "withLastLocation", function() { return __WEBPACK_IMPORTED_MODULE_0__withLastLocation__["a"]; }); | ||
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__LastLocationProvider__ = __webpack_require__(2); | ||
/* harmony reexport (binding) */ __webpack_require__.d(__webpack_exports__, "LastLocationProvider", function() { return __WEBPACK_IMPORTED_MODULE_1__LastLocationProvider__["a"]; }); | ||
/***/ }), | ||
/* 4 */ | ||
/***/ (function(module, __webpack_exports__, __webpack_require__) { | ||
"use strict"; | ||
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0_react__ = __webpack_require__(0); | ||
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0_react___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0_react__); | ||
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1_react_router_dom__ = __webpack_require__(1); | ||
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1_react_router_dom___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_1_react_router_dom__); | ||
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_2__LastLocationProvider__ = __webpack_require__(2); | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
function getDisplayName(WrappedComponent) { | ||
return WrappedComponent.displayName || WrappedComponent.name || 'Component'; | ||
} | ||
/* harmony default export */ __webpack_exports__["a"] = (function (WrappedComponent) { | ||
var WithLastLocation = function WithLastLocation(props) { | ||
return __WEBPACK_IMPORTED_MODULE_0_react___default.a.createElement(WrappedComponent, _extends({ lastLocation: Object(__WEBPACK_IMPORTED_MODULE_2__LastLocationProvider__["b" /* getLastLocation */])() }, props)); | ||
}; | ||
WithLastLocation.displayName = 'WithLastLocation(' + getDisplayName(WrappedComponent) + ')'; | ||
return Object(__WEBPACK_IMPORTED_MODULE_1_react_router_dom__["withRouter"])(WithLastLocation); | ||
}); | ||
/***/ }), | ||
/* 5 */ | ||
/***/ (function(module, exports) { | ||
module.exports = require("prop-types"); | ||
/***/ }) | ||
/******/ ]); | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("react"),require("prop-types"),require("react-router-dom")):"function"==typeof define&&define.amd?define(["react","prop-types","react-router-dom"],e):"object"==typeof exports?exports.ReactRouterLastLocation=e(require("react"),require("prop-types"),require("react-router-dom")):t.ReactRouterLastLocation=e(t.react,t["prop-types"],t["react-router-dom"])}(window,function(t,e,n){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var o=e[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)n.d(r,o,function(e){return t[e]}.bind(null,o));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=3)}([function(e,n){e.exports=t},function(t,n){t.exports=e},function(t,e){t.exports=n},function(t,e,n){"use strict";n.r(e);var r=n(0),o=n.n(r),a=n(2),u=n(1),c=n.n(u),i=Object(r.createContext)({});function f(t){return(f="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function l(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function p(t){return(p=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function s(t,e){return(s=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function y(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function b(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}var d=null,m=function(t){var e=t.location,n=t.nextLocation,r=t.watchOnlyPathname;null!==e&&n!==e&&(r&&e.pathname===n.pathname||(d=function(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{},r=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(r=r.concat(Object.getOwnPropertySymbols(n).filter(function(t){return Object.getOwnPropertyDescriptor(n,t).enumerable}))),r.forEach(function(e){b(t,e,n[e])})}return t}({},e)))},h=function(t){function e(){var t,n,r,o;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,e);for(var a=arguments.length,u=new Array(a),c=0;c<a;c++)u[c]=arguments[c];return r=this,n=!(o=(t=p(e)).call.apply(t,[this].concat(u)))||"object"!==f(o)&&"function"!=typeof o?y(r):o,b(y(y(n)),"state",{currentLocation:null}),n}var n,a,u;return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&s(t,e)}(e,r["Component"]),n=e,u=[{key:"getDerivedStateFromProps",value:function(t,e){return m({location:e.currentLocation,nextLocation:t.location,watchOnlyPathname:t.watchOnlyPathname}),{currentLocation:t.location}}}],(a=[{key:"render",value:function(){var t=this.props.children;return o.a.createElement(i.Provider,{value:d},t)}}])&&l(n.prototype,a),u&&l(n,u),e}();b(h,"propTypes",{watchOnlyPathname:c.a.bool,location:c.a.shape({pathname:c.a.string,search:c.a.string,hash:c.a.string,key:c.a.string,state:c.a.object}).isRequired,children:c.a.node.isRequired}),b(h,"defaultProps",{watchOnlyPathname:!1});var O=Object(a.withRouter)(h);function v(){return(v=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t}).apply(this,arguments)}var j=function(t){var e=function(e){return o.a.createElement(t,v({lastLocation:d},e))};return e.displayName="WithLastLocation(".concat(function(t){return t.displayName||t.name||"Component"}(t),")"),Object(a.withRouter)(e)};function w(){return Object(r.useContext)(i)}n.d(e,"withLastLocation",function(){return j}),n.d(e,"LastLocationProvider",function(){return O}),n.d(e,"useLastLocation",function(){return w})}])}); |
@@ -21,3 +21,5 @@ { | ||
}, | ||
"setupTestFrameworkScriptFile": "<rootDir>/tests/setup.js" | ||
"setupFilesAfterEnv": [ | ||
"<rootDir>/tests/setup.js" | ||
] | ||
} |
{ | ||
"name": "react-router-last-location", | ||
"version": "1.1.0", | ||
"version": "2.0.0-beta.0", | ||
"description": "Provides access to the last location in react + react-router (v4.x) apps. Useful for handling internal routing. Easily prevent leaving your app by users.", | ||
@@ -24,3 +24,3 @@ "main": "dist/index.js", | ||
"prebuild": "npm run clean", | ||
"build": "webpack --config webpack.config.js --env.prod --define process.env.NODE_ENV=\"'production'\"", | ||
"build": "webpack --config webpack.config.js --env.prod", | ||
"prepublish": "npm run build", | ||
@@ -41,29 +41,44 @@ "test": "jest --config=jest.config.json", | ||
"devDependencies": { | ||
"babel-core": "^6.24.1", | ||
"babel-eslint": "^8.1.2", | ||
"babel-jest": "^22.0.4", | ||
"babel-loader": "^7.0.0", | ||
"babel-plugin-transform-es2015-modules-commonjs": "^6.24.1", | ||
"babel-preset-es2015": "^6.24.1", | ||
"babel-preset-react": "^6.24.1", | ||
"babel-preset-stage-2": "^6.24.1", | ||
"coveralls": "^2.13.1", | ||
"enzyme": "^3.3.0", | ||
"enzyme-adapter-react-16": "^1.1.1", | ||
"eslint": "^4.14.0", | ||
"eslint-config-airbnb": "^16.1.0", | ||
"eslint-plugin-import": "^2.7.0", | ||
"eslint-plugin-jsx-a11y": "^6.0.3", | ||
"eslint-plugin-react": "^7.5.1", | ||
"html-webpack-plugin": "^2.28.0", | ||
"jest": "^22.0.4", | ||
"npm-run-all": "^4.1.2", | ||
"react": "^16.2.0", | ||
"react-dom": "^16.2.0", | ||
"react-router-dom": "^4.1.1", | ||
"react-test-renderer": "16.2.0", | ||
"serve": "^6.4.3", | ||
"webpack": "^3.10.0", | ||
"webpack-dev-server": "^2.9.7" | ||
"@babel/core": "^7.0.0", | ||
"@babel/plugin-proposal-class-properties": "^7.0.0", | ||
"@babel/plugin-proposal-decorators": "^7.0.0", | ||
"@babel/plugin-proposal-export-namespace-from": "^7.0.0", | ||
"@babel/plugin-proposal-function-sent": "^7.0.0", | ||
"@babel/plugin-proposal-json-strings": "^7.0.0", | ||
"@babel/plugin-proposal-numeric-separator": "^7.0.0", | ||
"@babel/plugin-proposal-throw-expressions": "^7.0.0", | ||
"@babel/plugin-syntax-dynamic-import": "^7.0.0", | ||
"@babel/plugin-syntax-import-meta": "^7.0.0", | ||
"@babel/plugin-transform-modules-commonjs": "^7.0.0", | ||
"@babel/preset-env": "^7.0.0", | ||
"@babel/preset-react": "^7.0.0", | ||
"babel-core": "^7.0.0-bridge.0", | ||
"babel-eslint": "^10.0.1", | ||
"babel-jest": "^24.1.0", | ||
"babel-loader": "^8.0.0", | ||
"coveralls": "^3.0.2", | ||
"css-hot-loader": "^1.4.3", | ||
"css-loader": "^2.1.0", | ||
"enzyme": "^3.8.0", | ||
"enzyme-adapter-react-16": "^1.9.1", | ||
"eslint": "^5.3.0", | ||
"eslint-config-airbnb": "^17.1.0", | ||
"eslint-plugin-import": "^2.14.0", | ||
"eslint-plugin-jsx-a11y": "^6.1.1", | ||
"eslint-plugin-react": "^7.11.0", | ||
"html-webpack-plugin": "^3.2.0", | ||
"jest": "^24.1.0", | ||
"linaria": "^1.1.0", | ||
"mini-css-extract-plugin": "^0.5.0", | ||
"npm-run-all": "^4.1.5", | ||
"react": "^16.8.1", | ||
"react-dom": "^16.8.1", | ||
"react-router-dom": "^4.3.1", | ||
"react-test-renderer": "16.8.1", | ||
"serve": "^10.1.2", | ||
"webpack": "^4.29.3", | ||
"webpack-cli": "^3.2.3", | ||
"webpack-dev-server": "^3.1.14", | ||
"webpack-merge": "^4.2.1" | ||
} | ||
} |
@@ -6,3 +6,5 @@ [![Build Status](https://travis-ci.org/hinok/react-router-last-location.svg?branch=master)](https://travis-ci.org/hinok/react-router-last-location) | ||
- Provides access to the last location in react + react-router (v4.x) apps. | ||
- Provides access to the last location in `react` + `react-router (v4.x)` applications. | ||
- ❤️ Using [`hooks`](https://reactjs.org/docs/hooks-overview.html)? If yes, `useLastLocation`. | ||
- 💉 Using [`HOC`](https://reactjs.org/docs/higher-order-components.html)? - If yes, `withLastLocation`. | ||
- Useful for handling internal routing. | ||
@@ -65,5 +67,30 @@ - Easily prevent leaving your app by users. | ||
### Use hook `useLastLocation` to get `lastLocation`. | ||
`./components/Logger`, [see example](https://github.com/hinok/react-router-last-location/blob/eb552e0a82df6000ba140d8f20627b8bc68716b6/example/src/components/LoggerHooks/index.js) | ||
```jsx | ||
import React from 'react'; | ||
import { useLastLocation } from 'react-router-last-location'; | ||
const Logger = () => { | ||
const lastLocation = useLastLocation(); | ||
return ( | ||
<div> | ||
<h2>Logger!</h2> | ||
<pre> | ||
{JSON.stringify(lastLocation)} | ||
</pre> | ||
</div> | ||
); | ||
}; | ||
export default LoggerHooks; | ||
``` | ||
### Use HOC `withLastLocation` to get `lastLocation` prop. | ||
`<Logger />` | ||
`./components/Logger`, [see example](https://github.com/hinok/react-router-last-location/blob/eb552e0a82df6000ba140d8f20627b8bc68716b6/example/src/components/LoggerHOC/index.js) | ||
@@ -92,2 +119,2 @@ ```jsx | ||
Stores only the last route when pathname is changed | ||
Stores the last route only when pathname has changed |
export { default as withLastLocation } from './withLastLocation'; | ||
export { default as LastLocationProvider } from './LastLocationProvider'; | ||
export { default as useLastLocation } from './useLastLocation'; |
@@ -1,9 +0,27 @@ | ||
import { Component } from 'react'; | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { withRouter } from 'react-router-dom'; | ||
import LastLocationContext from './LastLocationContext'; | ||
let lastLocation = null; | ||
const updateLastLocation = ({ location, nextLocation, watchOnlyPathname }) => { | ||
if (location === null) { | ||
return; | ||
} | ||
if (nextLocation === location) { | ||
return; | ||
} | ||
if (watchOnlyPathname && location.pathname === nextLocation.pathname) { | ||
return; | ||
} | ||
lastLocation = { ...location }; | ||
}; | ||
class LastLocationProvider extends Component { | ||
static propTypes = { | ||
// eslint-disable-next-line react/no-unused-prop-types | ||
watchOnlyPathname: PropTypes.bool, | ||
@@ -24,25 +42,26 @@ location: PropTypes.shape({ | ||
componentWillReceiveProps(nextProps) { | ||
if (this.props.location === nextProps.location) { | ||
return; | ||
} | ||
static getDerivedStateFromProps(props, state) { | ||
updateLastLocation({ | ||
location: state.currentLocation, | ||
nextLocation: props.location, | ||
watchOnlyPathname: props.watchOnlyPathname, | ||
}); | ||
if (!this.props.watchOnlyPathname) { | ||
lastLocation = { | ||
...this.props.location, | ||
}; | ||
return; | ||
} | ||
if (this.props.location.pathname === nextProps.location.pathname) { | ||
return; | ||
} | ||
lastLocation = { | ||
...this.props.location, | ||
return { | ||
currentLocation: props.location, | ||
}; | ||
} | ||
state = { | ||
currentLocation: null, | ||
}; | ||
render() { | ||
return this.props.children; | ||
const { children } = this.props; | ||
return ( | ||
<LastLocationContext.Provider value={lastLocation}> | ||
{children} | ||
</LastLocationContext.Provider> | ||
); | ||
} | ||
@@ -52,2 +71,7 @@ } | ||
export const getLastLocation = () => lastLocation; | ||
export const setLastLocation = (nextLastLocation) => { | ||
lastLocation = nextLastLocation; | ||
}; | ||
export default withRouter(LastLocationProvider); |
@@ -6,21 +6,31 @@ /* eslint-disable global-require */ | ||
const prepareTest = ({ watchOnlyPathname } = {}) => { | ||
jest.resetModules(); | ||
const { default: LastLocationProvider, getLastLocation } = require('./LastLocationProvider'); | ||
const wrapper = mount( | ||
React.createElement(props => ( | ||
<MemoryRouter initialEntries={['/']}> | ||
<LastLocationProvider watchOnlyPathname={watchOnlyPathname} {...props}> | ||
<div>Test</div> | ||
</LastLocationProvider> | ||
</MemoryRouter> | ||
)), | ||
); | ||
const history = wrapper.find('LastLocationProvider').prop('history'); | ||
return { | ||
wrapper, | ||
history, | ||
getLastLocation, | ||
}; | ||
}; | ||
describe('LastLocationProvider', () => { | ||
describe('When watchOnlyPathname is false', () => { | ||
it('should set lastLocation each time when route is changed', () => { | ||
jest.resetModules(); | ||
const { getLastLocation, history } = prepareTest(); | ||
const { | ||
default: LastLocationProvider, | ||
getLastLocation, | ||
} = require('./LastLocationProvider'); | ||
const wrapper = mount(( | ||
<MemoryRouter initialEntries={['/']}> | ||
<LastLocationProvider> | ||
<div>Test</div> | ||
</LastLocationProvider> | ||
</MemoryRouter> | ||
)); | ||
const history = wrapper.find('LastLocationProvider').prop('history'); | ||
expect(getLastLocation()).toBeNull(); | ||
@@ -38,18 +48,4 @@ history.push('/test-1'); | ||
it('should set lastLocation each time when pathname in location is changed', () => { | ||
jest.resetModules(); | ||
const { getLastLocation, history } = prepareTest({ watchOnlyPathname: true }); | ||
const { | ||
default: LastLocationProvider, | ||
getLastLocation, | ||
} = require('./LastLocationProvider'); | ||
const wrapper = mount(( | ||
<MemoryRouter initialEntries={['/']}> | ||
<LastLocationProvider watchOnlyPathname> | ||
<div>Test</div> | ||
</LastLocationProvider> | ||
</MemoryRouter> | ||
)); | ||
const history = wrapper.find('LastLocationProvider').prop('history'); | ||
expect(getLastLocation()).toBeNull(); | ||
@@ -64,2 +60,20 @@ history.push('/test-1'); | ||
}); | ||
it('should do nothing if application is rerendered and location is the same', () => { | ||
const { wrapper, getLastLocation, history } = prepareTest(); | ||
history.push('/test-1'); | ||
history.push('/test-2'); | ||
const lastLocationPrev = getLastLocation(); | ||
/** | ||
* This one is a bit tricky. I want to test case when `getDerivedStateFromProps` would be | ||
* called when location is not changing, e.g. any other prop is changing... | ||
* @see https://github.com/airbnb/enzyme/issues/1925#issuecomment-463248558 | ||
*/ | ||
wrapper.setProps({ pleaseRerenderLastLocationProvider: true }); | ||
const lastLocationNext = getLastLocation(); | ||
expect(lastLocationPrev).toBe(lastLocationNext); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
380149
24
118
41
363
3
1