From 91fdca6e77d099ff4f3a12ba7517f429a5b5ed57 Mon Sep 17 00:00:00 2001 From: Fabiana Fonseca Date: Wed, 7 Oct 2020 17:17:19 -0300 Subject: [PATCH] fixing listener use on component lifecycle --- react/index.tsx | 15 ++++++++++----- react/package.json | 2 +- react/yarn.lock | 8 ++++---- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/react/index.tsx b/react/index.tsx index 2984dff..cbdad3b 100644 --- a/react/index.tsx +++ b/react/index.tsx @@ -16,14 +16,19 @@ class CheckoutButtonExample extends Component<{}, CheckoutButtonExampleState> { } } - listenOrderFormUpdated() { - $(window).on('orderFormUpdated.vtex', (_: any, orderForm: OrderForm) => - this.setState({ orderForm }) - ) + setOrderForm = (_: any, orderForm: OrderForm) => { + this.setState({ orderForm }) + } + + componentDidMount() { + $(window).on('orderFormUpdated.vtex', this.setOrderForm) + } + + componentWillUnmount() { + $(window).off('orderFormUpdated.vtex', this.setOrderForm) } render() { - this.listenOrderFormUpdated() console.log(window.vtex.i18n.getLocale()) return diff --git a/react/package.json b/react/package.json index 2c20c70..24f4690 100644 --- a/react/package.json +++ b/react/package.json @@ -25,6 +25,6 @@ "react-apollo": "^2.0.4", "react-dom": "^16.2.0", "react-intl": "^2.4.0", - "typescript": "3.8.3" + "typescript": "3.9.7" } } diff --git a/react/yarn.lock b/react/yarn.lock index 12fc8c2..8869880 100644 --- a/react/yarn.lock +++ b/react/yarn.lock @@ -2525,10 +2525,10 @@ typed-styles@^0.0.7: resolved "https://registry.yarnpkg.com/typed-styles/-/typed-styles-0.0.7.tgz#93392a008794c4595119ff62dde6809dbc40a3d9" integrity sha512-pzP0PWoZUhsECYjABgCGQlRGL1n7tOHsgwYv3oIiEpJwGhFTuty/YNeduxQYzXXa3Ge5BdT6sHYIQYpl4uJ+5Q== -typescript@3.8.3: - version "3.8.3" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.8.3.tgz#409eb8544ea0335711205869ec458ab109ee1061" - integrity sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w== +typescript@3.9.7: + version "3.9.7" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.9.7.tgz#98d600a5ebdc38f40cb277522f12dc800e9e25fa" + integrity sha512-BLbiRkiBzAwsjut4x/dsibSTB6yWpwT5qWmC2OfuCg3GgVQCSgMs4vEctYPhsaGtd0AeuuHMkjZ2h2WG8MSzRw== uncontrollable@^6.0.0: version "6.2.3"