Skip to content

Instantly share code, notes, and snippets.

@matijagalina
Created December 31, 2019 15:00
Show Gist options
  • Save matijagalina/a910ffcafeeb12563231cbef5116c4fd to your computer and use it in GitHub Desktop.
Save matijagalina/a910ffcafeeb12563231cbef5116c4fd to your computer and use it in GitHub Desktop.
Custom Prompt react-router
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
// "react-router": "^5.1.2"
// "react-router-dom": "^5.1.2"
<BrowserRouter
getUserConfirmation={(message, callback) => (
CustomPromptComponent(message, callback)
)}
>
...
</BrowserRouter>
// custom prompt message component
const CustomPromptComponent = (message, callback) => {
const container = document.createElement('div');
container.setAttribute('custom-confirmation-navigation', '');
document.body.appendChild(container);
const closeModal = (callbackState) => {
ReactDOM.unmountComponentAtNode(container);
callback(callbackState);
};
ReactDOM.render(
<SomeModalComponent
closeModal={() => closeModal(false)}
closeCb={() => closeModal(true)}
>
{message}
</SomeModalComponent>,
container,
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment