Skip to content

Commit 1ac12d1

Browse files
authored
Merge pull request #9 from xJkit/enhance/provider-rerender
Refactor Provider with bound object
2 parents b9dc3fb + 4131dc4 commit 1ac12d1

File tree

4 files changed

+31
-23
lines changed

4 files changed

+31
-23
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ ReactDom.render(
170170
| isShow | Boolean | false | While `when` prop is true, `isShow` will be true when routing transition occurs. You can put your dialog component here. |
171171
| handleOk | function | | Allow routing transition and make `isShow` to be **false** again. |
172172
| handleCancel | function | | Block routing transition and make `isShow` to be **false** again. |
173-
| pass | function | | Low-level api under `handleOk` and `handleCancel`; pass **true** will allow routing transitions, while pass **false** will not. |
173+
| 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. |
174174
175175
## License
176176

example/src/Containers/Settings.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ import React from 'react';
33
import Modal from '../components/Modal';
44
import GoodBye from 'react-goodbye';
55

6+
const styles = {
7+
btnStyle: {
8+
color: 'white',
9+
marginRight: '8px',
10+
padding: '8px 12px'
11+
}
12+
}
13+
614
class Settings extends React.Component {
715
constructor(props) {
816
super(props);
@@ -32,18 +40,19 @@ class Settings extends React.Component {
3240
</p>
3341
<div style={{ textAlign: 'right' }}>
3442
<button
43+
onClick={handleOk}
3544
style={{
36-
color: 'white',
37-
backgroundColor: 'red',
38-
marginRight: '8px',
39-
padding: '8px 12px'
40-
}} onClick={handleOk}>
45+
...styles.btnStyle,
46+
backgroundColor: 'red'
47+
}}
48+
>
4149
Leave
4250
</button>
4351
<button
4452
onClick={handleCancel}
4553
style={{
46-
padding: '8px 12px'
54+
...styles.btnStyle,
55+
color: 'black'
4756
}}
4857
>
4958
Stay

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-goodbye",
3-
"version": "0.2.1",
3+
"version": "0.3.0",
44
"keywords": [
55
"react",
66
"react-router",

src/GoodByeProvider.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ class Provider extends React.Component {
88
constructor(props) {
99
super(props);
1010
this.state = {
11-
isShow: false
11+
isShow: false,
12+
handleOk: this.handleOk,
13+
handleCancel: this.handleCancel,
14+
handlePass: this.handlePass
1215
};
1316
this.pass = undefined;
17+
1418
invariant(
1519
React.createContext,
1620
'react-goodbye only support React 16.3+ context api, please upgrade your react to the latest version.'
@@ -19,9 +23,7 @@ class Provider extends React.Component {
1923

2024
handleGetUserConfirm = (message, pass) => {
2125
this.pass = pass;
22-
this.setState({
23-
isShow: true
24-
});
26+
this.setState({ isShow: true });
2527
};
2628

2729
handleOk = () => {
@@ -34,19 +36,16 @@ class Provider extends React.Component {
3436
this.setState({ isShow: false });
3537
};
3638

39+
handlePass = bool => {
40+
this.pass(bool);
41+
this.setState({ isShow: false });
42+
}
43+
3744
render() {
45+
const { children } = this.props;
3846
return (
39-
<GoodByeContext.Provider
40-
value={{
41-
isShow: this.state.isShow,
42-
handleOk: this.handleOk,
43-
handleCancel: this.handleCancel,
44-
pass: this.pass
45-
}}
46-
>
47-
{this.props.children({
48-
handleGetUserConfirm: this.handleGetUserConfirm
49-
})}
47+
<GoodByeContext.Provider value={this.state}>
48+
{children({ handleGetUserConfirm: this.handleGetUserConfirm })}
5049
</GoodByeContext.Provider>
5150
);
5251
}

0 commit comments

Comments
 (0)