Skip to content
This repository has been archived by the owner on Mar 27, 2021. It is now read-only.

Replace use of deprecated lifecycle method componentWillReceiveProps #255

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions demo/demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,6 @@ class _PaymentRequestForm extends React.Component<
},
});

paymentRequest.on('token', ({complete, token, ...data}) => {
console.log('Received Stripe token: ', token);
console.log('Received customer information: ', data);
complete('success');
});

paymentRequest.canMakePayment().then((result) => {
this.setState({canMakePayment: !!result});
});

this.state = {
canMakePayment: false,
paymentRequest,
Expand All @@ -187,6 +177,18 @@ class _PaymentRequestForm extends React.Component<
paymentRequest: Object,
};

componentDidMount() {
this.state.paymentRequest.on('token', ({complete, token, ...data}) => {
console.log('Received Stripe token: ', token);
console.log('Received customer information: ', data);
complete('success');
});

this.state.paymentRequest.canMakePayment().then((result) => {
this.setState({canMakePayment: !!result});
});
}

render() {
return this.state.canMakePayment ? (
<PaymentRequestButtonElement
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"babel-preset-react": "^6.24.1",
"cross-env": "^5.0.5",
"doctoc": "^1.3.0",
"enzyme": "^2.9.1",
"enzyme": "^3.7.0",
"enzyme-adapter-react-15": "^1.1.1",
"eslint": "^4.6.1",
"eslint-config-prettier": "2.9.0",
"eslint-config-stripe": "^1.0.13",
Expand All @@ -74,5 +75,8 @@
"peerDependencies": {
"react": "^15.5.4 || ^16.0.0-0",
"react-dom": "^15.5.4 || ^16.0.0-0"
},
"jest": {
"setupTestFrameworkScriptFile": "<rootDir>/src/setupTests.js"
}
}
4 changes: 2 additions & 2 deletions src/components/Element.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ const Element = (
});
}

componentWillReceiveProps(nextProps: Props) {
const options = _extractOptions(nextProps);
componentDidUpdate() {
const options = _extractOptions(this.props);
if (
Object.keys(options).length !== 0 &&
!isEqual(options, this._options)
Expand Down
6 changes: 3 additions & 3 deletions src/components/Elements.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('Elements', () => {
</Elements>,
{context: syncContext}
);
const childContext = wrapper.node.getChildContext();
const childContext = wrapper.instance().getChildContext();
expect(Object.keys(childContext)).toEqual([
'addElementsLoadListener',
'registerElement',
Expand All @@ -52,7 +52,7 @@ describe('Elements', () => {
</Elements>,
{context: syncContext}
);
const childContext = wrapper.node.getChildContext();
const childContext = wrapper.instance().getChildContext();

const mockCallback = jest.fn();
childContext.addElementsLoadListener(mockCallback);
Expand All @@ -76,7 +76,7 @@ describe('Elements', () => {
</Elements>,
{context: asyncContext}
);
const childContext = wrapper.node.getChildContext();
const childContext = wrapper.instance().getChildContext();

const a = new Promise((resolve) =>
childContext.addElementsLoadListener((first) => {
Expand Down
6 changes: 3 additions & 3 deletions src/components/PaymentRequestButtonElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ class PaymentRequestButtonElement extends React.Component<Props> {
this._element.mount(this._ref);
});
}
componentWillReceiveProps(nextProps: Props) {
if (this.props.paymentRequest !== nextProps.paymentRequest) {
componentDidUpdate(prevProps: Props) {
if (this.props.paymentRequest !== prevProps.paymentRequest) {
console.warn(
'Unsupported prop change: paymentRequest is not a customizable property.'
);
}
const options = _extractOptions(nextProps);
const options = _extractOptions(this.props);
if (
Object.keys(options).length !== 0 &&
!shallowEqual(options, this._options)
Expand Down
14 changes: 7 additions & 7 deletions src/components/Provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,16 @@ export default class Provider extends React.Component<Props> {
}
}

componentWillReceiveProps(nextProps: Props) {
componentDidUpdate(prevProps: Props) {
const apiKeyChanged =
this.props.apiKey &&
nextProps.apiKey &&
this.props.apiKey !== nextProps.apiKey;
prevProps.apiKey &&
this.props.apiKey !== prevProps.apiKey;

const stripeInstanceChanged =
this.props.stripe &&
nextProps.stripe &&
this.props.stripe !== nextProps.stripe;
prevProps.stripe &&
this.props.stripe !== prevProps.stripe;
if (
!this._didWarn &&
(apiKeyChanged || stripeInstanceChanged) &&
Expand All @@ -167,10 +167,10 @@ export default class Provider extends React.Component<Props> {
return;
}

if (!this._didWakeUpListeners && nextProps.stripe) {
if (!this._didWakeUpListeners && this.props.stripe) {
// Wake up the listeners if we've finally been given a StripeShape
this._didWakeUpListeners = true;
const stripe = ensureStripeShape(nextProps.stripe);
const stripe = ensureStripeShape(this.props.stripe);
this._meta.stripe = stripe;
this._listeners.forEach((fn) => {
fn(stripe);
Expand Down
14 changes: 7 additions & 7 deletions src/components/Provider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('StripeProvider', () => {
<form />
</StripeProvider>
);
const childContext = wrapper.node.getChildContext();
const childContext = wrapper.instance().getChildContext();
expect(childContext).toEqual({stripe: stripeMockResult, tag: 'sync'});
});

Expand All @@ -112,7 +112,7 @@ describe('StripeProvider', () => {
<form />
</StripeProvider>
);
const childContext = wrapper.node.getChildContext();
const childContext = wrapper.instance().getChildContext();
expect(childContext).toEqual({stripe: stripeMockResult, tag: 'sync'});
});

Expand All @@ -122,7 +122,7 @@ describe('StripeProvider', () => {
<form />
</StripeProvider>
);
const childContext = wrapper.node.getChildContext();
const childContext = wrapper.instance().getChildContext();
expect(childContext).toHaveProperty('addStripeLoadListener');
expect(childContext).toHaveProperty('tag', 'async');
});
Expand All @@ -134,7 +134,7 @@ describe('StripeProvider', () => {
</StripeProvider>
);

const childContext = wrapper.node.getChildContext();
const childContext = wrapper.instance().getChildContext();
childContext.addStripeLoadListener((stripe) => {
expect(stripe).toEqual(stripeMockResult);
done();
Expand All @@ -152,7 +152,7 @@ describe('StripeProvider', () => {
<form />
</StripeProvider>
);
let childContext = wrapper.node.getChildContext();
let childContext = wrapper.instance().getChildContext();
const keyOneInstance = childContext.stripe;
expect(keyOneInstance).toBeTruthy();

Expand All @@ -162,7 +162,7 @@ describe('StripeProvider', () => {
<form />
</StripeProvider>
);
childContext = wrapper.node.getChildContext();
childContext = wrapper.instance().getChildContext();
expect(childContext.stripe).toBe(keyOneInstance);

// Create another, but with a different key!
Expand All @@ -171,7 +171,7 @@ describe('StripeProvider', () => {
<form />
</StripeProvider>
);
childContext = wrapper.node.getChildContext();
childContext = wrapper.instance().getChildContext();
expect(childContext.stripe).not.toBe(keyOneInstance);
});

Expand Down
4 changes: 2 additions & 2 deletions src/components/inject.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ describe('injectStripe()', () => {
context,
});

expect(() => wrapper.node.getWrappedInstance()).toThrow(
expect(() => wrapper.instance().getWrappedInstance()).toThrow(
'To access the wrapped instance, the `{withRef: true}` option must be set when calling `injectStripe()`'
);
});
Expand All @@ -289,7 +289,7 @@ describe('injectStripe()', () => {
});

expect(
wrapper.node.getWrappedInstance() instanceof WrappedClassComponent
wrapper.instance().getWrappedInstance() instanceof WrappedClassComponent
).toBe(true);
});
});
Expand Down
5 changes: 5 additions & 0 deletions src/setupTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @noflow
import {configure} from 'enzyme';
import Adapter from 'enzyme-adapter-react-15';

configure({adapter: new Adapter()});
Loading