Skip to content

Commit 821088d

Browse files
author
Mattia Panzeri
committed
feat(Context): Add 'Provider' and 'HOC' to simplify use
1 parent 2bc49a4 commit 821088d

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"react-native": ">=0.54.0"
1616
},
1717
"dependencies": {
18+
"hoist-non-react-statics": "^3.0.1",
1819
"react-native-iphone-x-helper": "^1.2.0",
1920
"react-native-swipe-gestures": "^1.0.2"
2021
},

src/context.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react';
2+
3+
const context = React.createContext(() => {});
4+
5+
export default context;

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
export { default as Notification } from './notification';
2+
3+
export { default as Provider } from './provider';
4+
export { default as withInAppNotification } from './with-in-app-notification.hoc';

src/provider.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React, { Children } from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
import Context from './context';
5+
import Notification from './notification';
6+
7+
class Provider extends React.PureComponent {
8+
constructor(props) {
9+
super(props);
10+
11+
this.showNotification = this.showNotification.bind(this);
12+
}
13+
14+
showNotification(notificationOptions) {
15+
if (this.notification) {
16+
this.notification.show(notificationOptions);
17+
}
18+
}
19+
20+
render() {
21+
return (
22+
<Context.Provider value={this.showNotification}>
23+
{Children.only(this.props.children)}
24+
<Notification
25+
ref={(ref) => {
26+
this.notification = ref;
27+
}}
28+
/>
29+
</Context.Provider>
30+
);
31+
}
32+
}
33+
34+
Provider.propTypes = {
35+
children: PropTypes.element.isRequired,
36+
};
37+
38+
export default Provider;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React from 'react';
2+
3+
import hoistNonReactStatic from 'hoist-non-react-statics';
4+
5+
import Context from '~/react-native-in-app-notification/src/context';
6+
7+
function withInAppNotification(WrappedComponent) {
8+
class Enhanced extends React.PureComponent {
9+
render() {
10+
return (
11+
<Context.Consumer>
12+
{showNotification => (
13+
<WrappedComponent
14+
{...this.props}
15+
showNotification={showNotification}
16+
/>
17+
)}
18+
</Context.Consumer>
19+
);
20+
}
21+
}
22+
23+
// Pass over static props
24+
hoistNonReactStatic(Enhanced, WrappedComponent);
25+
26+
return Enhanced;
27+
}
28+
29+
export default withInAppNotification;

0 commit comments

Comments
 (0)