• Stars
    star
    119
  • Rank 297,930 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A save reminder component for react router

React GoodBye

A save reminder component for react router v4.

npm version circle ci coverage license mit

react-goodbye is a save reminder utility to prevent routing transition before you leave without saving changes.

Please check the demo page for more information.

Install

NPM

npm install --save react-goodbye

or you can use yarn:

yarn add react-goodbye

note: react-goodbye uses React 16.3 new context api under the hood. Therefore, only React v16.3+ are supported.

Usage

  • Decorate your router provider from react-router using withGoodBye:
import { BrowserRouter } from 'react-router-dom';
import { withGoodBye } from 'react-goodbye';

const EnhancedRouter = withGoodBye(BrowserRouter);

ReactDOM.render(
  <EnhancedRouter>
    <App />
  </EnhancedRouter>,
  document.getElementById('root')
);
  • Import GoodBye component under the page you want with save reminder:
import React from 'react';
import GoodBye from 'react-goodbye';

import Modal from './path/to/your/modal/component';

class Page extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      initialValue: props.initialValue,
      currentValue: props.initialValue
    };
  }
  render() {
    return (
      <div>
        <GoodBye when={this.state.initialValue !== this.state.currentValue}>
          {({ isShow, handleOk, handleCancel }) =>
            isShow && (
              <Modal>
                <h3>Settings Changed</h3>
                <p>
                  You change the page without saving any data. Do you want to
                  leave?
                </p>
                <button onClick={handleOk}>Yes</button>
                <button onClick={handleCancel}>No</button>
              </Modal>
            )
          }
        </GoodBye>
        <input
          type="input"
          value={this.state.currentValue}
          onChange={evt => this.setState({ currentValue: evt.target.value })}
        />
      </div>
    );
  }
}

API Reference

withGoodBye

withGoodBye uses higher order component pattern to inject the getUserConfirmation handle function prop on the react-router provider. Use this HoC to decorate the router providers like BrowserRouter, HashRouter or low-level Router:

import { withGoodBye } from 'react-goodbye';
import { Router } from 'react-router';

const EnhancedRouter = withGoodBye(Router);

ReactDom.render(
  <EnhancedRouter>
    <App />
  </EnhancedRouter>
);

Provider

If you prefer render props pattern, you can use this Provider component like so:

import { Provider as GoodByeProvider } from 'react-goodbye';
import { BrowserRouter } from 'react-router-dom';

ReactDom.render(
  <GoodByeProvider>
    {({ handleGetUserConfirm }) => (
      <BrowserRouter getUserConfirmation={handleGetUserConfirm}>
        <App />
      </BrowserRouter>
    )}
  </GoodByeProvider>
);

GoodBye

GoodBye is the consumer component of the GoodBye context. This component must be in the subtree of Provider or decorated router provider.

props type default description
when Boolean false Make render props isShow to be true or false when routing transition occurs.
alertBeforeUnload Boolean false Turn on the browser alert. Technically, when you refresh or close browser window, only browser itself can detect and popup alert for you. If you want to remind the user when doing actions above, turn on this option.
alertMessage String '' Custom browser alert messages. Note that this option only works for IE.
conditionalPrompt func Custom callback to show the prompt conditionally based on the location. The function receives the location and you can return true to allow the transition or false to show the prompt.

react-goodbye will handle all of the code logic for you. Use provided render props to show whatever you want (modal, lightbox, dialog, popup, etc)

render props type default description
isShow Boolean false While when prop is true, isShow will be true when routing transition occurs. You can put your dialog component here.
handleOk function Allow routing transition and make isShow to be false again.
handleCancel function Block routing transition and make isShow to be false again.
handlePass function Low-level api under handleOk and handleCancel; pass true or false will allow/block routing transitions. Use this function to do your additional actions.

License

MIT ยฉ xJkit

More Repositories

1

electron-tray-app

Yet another demo for desktop app in tray icon built from Electron
JavaScript
9
star
2

modern-web-2018-demo

A demo repo for Modern Web 2018 Conference in Taipei.
JavaScript
8
star
3

study-notes

Just my pace of youth.
Haskell
7
star
4

react-d3-notes

Examples for rendering different type of charts by React and D3.
JavaScript
3
star
5

react-redux-rp

An awesome, declarative render props version of connect
JavaScript
3
star
6

puppeteer-demo

A demo for website scrapping by my puppet :>
JavaScript
3
star
7

tw-railway-killer

A handy automation tool to book the Taiwan railway tickets in command line interface.
TypeScript
3
star
8

trendmicro-fed-talk-pattern-hoc

A sharing about higher order component pattern in TrendMicro front end group
JavaScript
2
star
9

learn-threejs-in-react-way

Learn Three.js in React with declarative, component-based and more functional way. https://xjkit.github.io/learn-threejs-in-react-way/
JavaScript
2
star
10

todos-desktop-app

A todo desktop app built from Electron
JavaScript
1
star
11

chatbot-sweet-home

ๆฏๅคฉๅ•ๆœƒไธๆœƒๅ›žๅฎถๅƒ้ฃฏ็š„ LINE ๆฉŸๅ™จไบบ
JavaScript
1
star
12

xJkit

1
star
13

studio-template-previews

Preview pages for studio template.
JavaScript
1
star
14

sweetjohn_link_page

๐Ÿฅ Master link page for Sweet John music band
JavaScript
1
star
15

fish-shell-mac

Replace default BASH with friendly-interactive shell: fish.
1
star
16

ThunkForm

Demo Redux Thunk for Multiple Async Actions
JavaScript
1
star
17

factorial-benchmark

Do the factorial benchmark comparison in JavaScript
JavaScript
1
star
18

React-GAS-Starter

Start a Google Apps Script (GAS) React Project
JavaScript
1
star
19

xjkit.github.io

The Suite John blog
JavaScript
1
star
20

j-album-app

An Instagram-like app demo in react native
JavaScript
1
star
21

linebot-echo

A linebot echo example built with netlify-functions, TypeScript and Express.js
TypeScript
1
star
22

sanity-nextjs-landing-pages

Next.js landing pages
JavaScript
1
star
23

ad-creator-engine

Tools for advertisement creation
JavaScript
1
star
24

SweetJohnIOT

A HouseKeeper Mobile App with Bluetooth Low Energy feature built-in.
TypeScript
1
star
25

nft-card-example

Created with CodeSandbox
JavaScript
1
star
26

networking_toolkit

Use Python to write some networking toolkits.
Python
1
star
27

full-stack-react-demo

A monorepo demo for server-side rendering React app.
JavaScript
1
star
28

form-validation-demo

A demo project for different implementation possibilities to validate form.
JavaScript
1
star