GithubHelp home page GithubHelp logo

sshyam-gupta / react-router-prompt Goto Github PK

View Code? Open in Web Editor NEW
73.0 3.0 14.0 726 KB

A replacement component for the react-router 6 `Prompt`. Allows for more flexible dialogs.

Home Page: https://codesandbox.io/s/react-router-prompt-example-react-router-6-7-y9ug7z?file=/src/App.js

License: MIT License

HTML 12.80% TypeScript 87.20%
prompt react-prompt react-router react-router-dom-v6 react-router-v6 hacktoberfest

react-router-prompt's Introduction

react-router-prompt 🚨

A component for the react-router 6 Prompt. Allows to create more flexible dialogs.

npm version

npm downloads

npm bundle size

Demo

Installation

Prerequisite

React-router-dom >= 6.19 and can be used only with data routers

pnpm add react-router-prompt

or with other package manager like yarn

yarn add react-router-prompt

Basic Usage

<ReactRouterPrompt when={isDirty}>
  {({ isActive, onConfirm, onCancel }) => (
    <Modal show={isActive}>
      <div>
        <p>Do you really want to leave?</p>
        <button onClick={onCancel}>Cancel</button>
        <button onClick={onConfirm}>Ok</button>
      </div>
    </Modal>
  )}
</ReactRouterPrompt>

Props

  1. when: boolean | BlockerFunction

    BlockerFunction = (args: {
      currentLocation: Location
      nextLocation: Location
      historyAction: HistoryAction
    }) => boolean
  2. beforeConfirm() : Promise<unknown> (Optional)

  3. beforeCancel() : Promise<unknown> (Optional)

Return values

  1. isActive: Boolean
  2. onConfirm(): void
  3. onCancel(): void

Note 🗒️

This version works with react-router-dom >=v6.19 Should be used within data routers

For react-router support (v6 - v6.2.x) please install v0.3.0

For react-router support (v6.7.x - v6.18.x) please install v0.5.4

Skipped support in middle due to breaking changes on react-router apis

Contributing

Contributions, issues and feature requests are always welcome! Feel free to check issues page.

Acknowledgements

Support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2023 Shyam Gupta ([email protected]) This project is MIT licensed.

About me

react-router-prompt's People

Contributors

andrewbusch7 avatar dependabot[bot] avatar erictompkins avatar jacquesg avatar renovate[bot] avatar skt-t1-byungi avatar sshyam-gupta avatar sweethuman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

react-router-prompt's Issues

Have to depend on @remix-run/router to pass a BlockerFunction in typescript?

Hi, I'm trying to pass a BlockerFunction as a when prop to ReactRouterPrompt in typescript.

Because I need to type my function definition, I need to import BlockerFunction from @remix-run/router, which means I need to depend on it, which is discouraged. I could use any for the type of args but not that keen on doing this either.

I think, if the ReactRouterPromptProps type is exported we can type it like ReactRouterPromptProps["when"]

I'm happy to raise the PR for this.

bug:使用浏览器的回退时,会优先执行children而不是when

详细描述一下,当我使用这个工具进行拦截路由跳转时,遇到浏览器回退和路由跳转不一样的效果。使用路由跳转时,先判断了when的条件,然后正常退出;但是用浏览器回退时并没有先判断条件,而是直接打开prompt组件又自己关闭了,但是页面却留在了当前页,再次点击浏览器回退时,才正常退出;
2081663758054_ pic

"isActive" prop is false even while "when" is true

I'm using a ReactRouterPrompt and it works most of the time. But when I go to the same page (the page that has the Prompt) but with different url parameters and after trying to leave that page, I do not see my popup.

I debugged and the "isActive" prop is false even if "when" and all conditions give true.

Any help with this?

Thanks!

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): update dependency @size-limit/preset-small-lib to v11.1.4
  • chore(deps): update dependency @vitejs/plugin-react-swc to v3.7.0
  • chore(deps): update dependency eslint to v8.57.0
  • chore(deps): update dependency eslint-plugin-jsx-a11y to v6.9.0
  • chore(deps): update dependency eslint-plugin-prettier to v5.2.1
  • chore(deps): update dependency husky to v9.1.3
  • chore(deps): update dependency prettier to v3.3.3
  • chore(deps): update dependency size-limit to v11.1.4
  • chore(deps): update dependency typescript to v5.5.4
  • chore(deps): update dependency eslint to v9
  • 🔐 Create all rate-limited PRs at once 🔐

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/codeql-analysis.yml
  • actions/checkout v4
  • github/codeql-action v2
  • github/codeql-action v2
  • github/codeql-action v2
.github/workflows/greetings.yml
  • actions/first-interaction v1
.github/workflows/publish-package.yml
  • actions/checkout v4
  • manovotny/github-releases-for-automated-package-publishing-action v2.0.1
  • pnpm/action-setup v2
  • actions/setup-node v4
.github/workflows/size-limit.yml
  • actions/checkout v4
  • pnpm/action-setup v2
  • andresz1/size-limit-action v1
npm
package.json
  • @size-limit/preset-small-lib ^11.0.2
  • @types/react ^18.2.55
  • @types/react-dom ^18.2.19
  • @typescript-eslint/eslint-plugin ^6.0.0
  • @typescript-eslint/parser ^7.0.1
  • @vitejs/plugin-react-swc ^3.6.0
  • eslint ^8.56.0
  • eslint-config-airbnb ^19.0.4
  • eslint-config-airbnb-typescript ^17.1.0
  • eslint-config-prettier ^9.1.0
  • eslint-plugin-import ^2.29.1
  • eslint-plugin-jsx-a11y ^6.8.0
  • eslint-plugin-prettier ^5.1.3
  • eslint-plugin-react ^7.33.2
  • eslint-plugin-react-hooks ^4.6.0
  • history ^5.3.0
  • husky ^9.0.11
  • path ^0.12.7
  • prettier ^3.2.5
  • react ^18.2.0
  • react-dom ^18.2.0
  • react-router-dom ^6.22.1
  • size-limit ^11.0.2
  • typescript ^5.3.3
  • vite ^5.1.3
  • vite-plugin-dts ^3.7.2
  • vite-tsconfig-paths ^4.3.1
  • react >=16.8
  • react-dom >=16.8
  • react-router-dom >=6.19

  • Check this box to trigger a request for Renovate to run again on this repository

"Cannot read properties of null (reading 'useEffect')" when used with React 17

Hello and thank you for your work. I gave the latest version a try, and it seems like there's a bug somewhere.

This happens to me when I use the following version of dependencies:

  • react 17.0.2
  • react-dom 17.0.2
  • react-router-dom
  • 6.8.1
  • react-router-prompt 0.5.1

Reproduction:

https://codesandbox.io/s/react-router-prompt-example-react-router-6-7-forked-p3n31e

I noticed that react and react-dom are listed both as peer dependencies and regular dependencies — perhaps that could be the cause of a mismatch?

Uncaught Error: useBlocker must be used within a data router

I am getting an error just with your example code

Error

Uncaught Error: useBlocker must be used within a data router

Code:

<ReactRouterPrompt when>
  {({ isActive, onConfirm, onCancel }) => (
    <Modal show={isActive}>
      <div>
        <p>Do you really want to leave?</p>
        <button onClick={onCancel}>Cancel</button>
        <button onClick={onConfirm}>Ok</button>
      </div>
    </Modal>
  )}
</ReactRouterPrompt>

Libs:

    "react-router-dom": "^6.14.2",
    "react-router-prompt": "^0.5.4",

`react-router-dom` load fails in Yarn berry.

error logs

RROR in ../../.yarn/__virtual__/react-router-prompt-virtual-bfafd500e5/0/cache/react-router-prompt-npm-0.3.0-14a024efe7-66dd019f29.zip/node_modules/react-router-prompt/dist/react-router-prompt.cjs.development.js 9:21-48
Module not found: Error: Can't resolve 'react-router-dom' in '/.yarn/__virtual__/react-router-prompt-virtual-bfafd500e5/0/cache/react-router-prompt-npm-0.3.0-14a024efe7-66dd019f29.zip/node_modules/react-router-prompt/dist'

The solution is to add react-router-dom to peer dependency.

Source Map Loading Errors

Hey there, im getting some errors with respect to this module. im not sure if it's related to the version of react-router im using:

react-router-dom: ^6.3.0

Failed to parse source map from '/src/node_modules/react-router-prompt/src/hooks/use-blocker.ts' file: Error: ENOENT: no such file or directory, open '/src/node_modules/react-router-prompt/src/hooks/use-blocker.ts'

Failed to parse source map from '/src/node_modules/react-router-prompt/src/hooks/use-confirm.ts' file: Error: ENOENT: no such file or directory, open '/src/node_modules/react-router-prompt/src/hooks/use-confirm.ts'

Failed to parse source map from '/src/node_modules/react-router-prompt/src/index.tsx' file: Error: ENOENT: no such file or directory, open '/src/node_modules/react-router-prompt/src/index.tsx'

Looking at the folder structure in node_modules it looks like maybe the files are not present in the module:

Screen Shot 2022-06-21 at 3 56 51 PM

No longer compatible with react-router

react-router v6.19 removed the unstable_ prefix from useBlocker, which breaks this library.

It should be updated along with the various out of date dependencies if it's still maintained.

Thank you

Bug?: `isActive` is always true when child function is called

As the title states, the isActive parameter is always true when the function gets called and when isActive should be false the function is just not called at all. I am assuming this is a bug since why would the parameter exist otherwise and this prevents alternate rendering and logic for when isActive should be false.

package version: 0.5.2
react-router-dom: 6.8.2

Incompatible invocation of BlockerFunction

As part of 6a1dc67a20c14c3d5952f8252519ada413c82eb6 the following change has been introduced:

        if (
          (typeof when === "boolean" && when === true) ||
          // @ts-ignore Reload case -- No location present
          (typeof when === "function" && when())
        ) {

Calling when without any arguments is not compatible with

  export declare type BlockerFunction = (args: {
    currentLocation: Location;
    nextLocation: Location;
    historyAction: HistoryAction;
}) => boolean;

Currently, this manifests as the following on page reload for me:

formactionbuttonblock.tsx:60 Uncaught TypeError: Cannot destructure property 'nextLocation' of 'undefined' as it is undefined.
    at when (:3000/src/components/formactionbuttonblock.tsx:32:21)
    at :3000/node_modules/.vite/deps/react-router-prompt.js?v=d73664e4:644:35

Demo doesn't load

Not a biggie,

Demo

has error

ModuleNotFoundError
Could not find module in path: 'react-dom/client' relative to '/src/index.js'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.