Skip to content

Commit 8a9118a

Browse files
authored
Merge pull request #47 from nathanstitt/context-upgrade
upgrade context usage to React 17+
2 parents 20aa8ef + 58e121d commit 8a9118a

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

src/braintree.jsx

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33
import Api from './api';
4+
import { Context } from './context'
5+
46

57
export default class Braintree extends React.Component {
68

@@ -21,13 +23,10 @@ export default class Braintree extends React.Component {
2123
tagName: 'div',
2224
}
2325

24-
static childContextTypes = {
25-
braintreeApi: PropTypes.instanceOf(Api),
26-
}
27-
2826
constructor(props) {
2927
super(props);
3028
this.api = new Api(props);
29+
this.contextValue = { braintreeApi: this.api }
3130
}
3231

3332
componentDidMount() {
@@ -49,18 +48,17 @@ export default class Braintree extends React.Component {
4948
return this.api.tokenize(options);
5049
}
5150

52-
getChildContext() {
53-
return { braintreeApi: this.api };
54-
}
55-
5651
render() {
5752
const { className: providedClass, tagName: Tag } = this.props;
5853
let className = 'braintree-hosted-fields-wrapper';
5954
if (providedClass) { className += ` ${providedClass}`; }
55+
6056
return (
61-
<Tag className={className}>
62-
{this.props.children}
63-
</Tag>
57+
<Context.Provider value={this.contextValue}>
58+
<Tag className={className}>
59+
{this.props.children}
60+
</Tag>
61+
</Context.Provider>
6462
);
6563
}
6664

src/context.js

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

src/field.jsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3-
import Api from './api';
3+
import { Context } from './context'
44

55
export default class BraintreeHostedField extends React.Component {
66

@@ -20,9 +20,7 @@ export default class BraintreeHostedField extends React.Component {
2020
prefill: PropTypes.string,
2121
}
2222

23-
static contextTypes = {
24-
braintreeApi: PropTypes.instanceOf(Api),
25-
}
23+
static contextType = Context
2624

2725
state = {}
2826

0 commit comments

Comments
 (0)