Skip to content

Instantly share code, notes, and snippets.

@kmwarter
Forked from robertgonzales/App.jsx
Created June 18, 2019 23:57
Show Gist options
  • Select an option

  • Save kmwarter/06d6f7701ea34f7e6d6bbd9fd318de2b to your computer and use it in GitHub Desktop.

Select an option

Save kmwarter/06d6f7701ea34f7e6d6bbd9fd318de2b to your computer and use it in GitHub Desktop.
How to make a custom Prompt (using getUserConfirmation) for React Router v4
// use Prompt like normal... magic happens in getUserConfirmation
class App extends Component {
render () {
return (
<Router getUserConfirmation={getUserConfirmation}>
{...}
<Prompt
when={formIsHalfFilledOut}
message="Are you sure you want to leave?"
/>
</Router>
)
}
}
ReactDOM.render(
<App />,
document.getElementById('root')
)
// default behavior uses window.confirm
const getUserConfirmation = (message, callback) => {
const modal = document.createElement('div')
document.body.appendChild(modal)
const withCleanup = (answer) => {
ReactDOM.unmountComponentAtNode(modal)
document.body.removeChild(modal)
callback(answer)
}
ReactDOM.render(
<UserConfirmation
message={message}
onCancel={() => withCleanup(false)}
onConfirm={() => withCleanup(true)}
/>,
modal
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment