Skip to content

Instantly share code, notes, and snippets.

View michchan's full-sized avatar

Michael Chan L. S. michchan

View GitHub Profile
@michchan
michchan / RouteLeavingGuard-class-usage-ca839f5faf39.jsx
Created July 15, 2022 03:07
RouteLeavingGuard Usage for Medium article ca839f5faf39
import React from ‘react’
import RouteLeavingGuard from 'components/RouteLeavingGuard'
export class LoginScene extends React.Component {
state = {
username: '',
password: '',
isDirty: false,
}
@michchan
michchan / RouteLeavingGuard-function-ts-ca839f5faf39.tsx
Created July 15, 2022 03:10
RouteLeavingGuard full implementation in TypeScript & React 16+ for Medium article ca839f5faf39
import { Location } from 'history';
import React, { useEffect, useState } from 'react';
import { Prompt } from 'react-router-dom';
import WarningDialog from './WarningDialog';
interface Props {
when?: boolean | undefined;
navigate: (path: string) => void;
shouldBlockNavigation: (location: Location) => boolean;