Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping third party JS libraries in ReactJS

Tags:

reactjs

I am trying to learn how to use 3rd party APIs like Google Map, or BrainTree Dropin UI, and make them into ReactJS components.

Along my research, I came across such an example of a project that wraps the Braintree (payment gateway) - https://github.com/Cretezy/braintree-web-drop-in-react/blob/master/src/index.js

Unfortunately I am not able to follow the code, in particular, I cannot understand the syntax in lines 22-23. They look like decorators, but I am not sure and/or how they work in this context.

like image 587
James Avatar asked Jan 03 '23 04:01

James


2 Answers

How to wrap third party libraries?

There a lot of resources about the topic. You can start with the official React documentation as a first one - Integrating with Other Libraries.

Also there's already created React wrappers / integrations, with the libraries you asked for:

  1. google-maps-react - A declarative Google Map React component using React. Also the library has a complete article how it works.
  2. braintree-dropin-react - React component to integrate Braintree Drop-In UI (V3).

What's the purpose of the code you mention?

It's just a property declaration. I guess it's there for the sake of clarity, but with or without wrapper, instance declarations, the code will still behave in the same way.

like image 81
Jordan Enev Avatar answered Jan 10 '23 00:01

Jordan Enev


The code you are talking about is the following:

class ... {
   ...

   wrapper;
   instance;

   ...
}

This is just a property declaration without giving a value, it's the same as:

class ... {
   ...

   wrapper = undefined;
   instance = undefined;

   ...
}

And nothing would actually happen if you removed that completely. If you inspect the code more, wrapper stores the DOM reference for the wrapping <div>, instance is the stored Braintree instance from componentDidMount.

It's possible ESLint rules enforce the declaration.

like image 45
Sulthan Avatar answered Jan 10 '23 00:01

Sulthan