Skip to content

Commit b3be46d

Browse files
author
Christian Fuentes
committed
Adding conditional prompt function to allow rendering Goodbye on some conditions
1 parent b9d2def commit b3be46d

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,19 @@ class GoodBye extends React.Component {
2727
}
2828

2929
render() {
30-
const { when, children } = this.props;
30+
const { when, children, conditionalPrompt } = this.props;
3131
return (
3232
<Fragment>
33-
<ReactRouterPrompt when={when} message="" />
33+
<ReactRouterPrompt
34+
when={when}
35+
message={location => {
36+
if (typeof conditionalPrompt === 'undefined' || conditionalPrompt(location) !== true) {
37+
return '';
38+
}
39+
40+
return true;
41+
}}
42+
/>
3443
<GoodByeContext.Consumer>
3544
{renderProps => children({ ...renderProps })}
3645
</GoodByeContext.Consumer>
@@ -41,6 +50,7 @@ class GoodBye extends React.Component {
4150

4251
GoodBye.propTypes = {
4352
when: PropTypes.bool,
53+
conditionalPrompt: PropTypes.func,
4454
alertBeforeUnload: PropTypes.bool,
4555
alertMessage: PropTypes.string,
4656
children: PropTypes.func.isRequired,

0 commit comments

Comments
 (0)