A thin React wrapper for Basis Theory JS SDK.
Using Node Package Manager
npm install --save @basis-theory/basis-theory-react
Using Yarn
yarn add @basis-theory/basis-theory-react
For a complete list of endpoints and examples, please refer to our React docs
Initializing the SDK is done via calling the useBasisTheory
hook with parameters:
import { useBasisTheory } from '@basis-theory/basis-theory-react';
export default function MyWrapper() {
const { bt, error } = useBasisTheory('<Public API Key>'); // replace with your application key
// instance stays undefined during initialization
if (bt) {
// able to call BasisTheory methods
}
if (error) {
// initialization error
}
}
You can pass the BasisTheoryReact
instance down to your component tree using BasisTheoryProvider
, and access it later calling the useBasisTheory
hook without any parameters:
import {
BasisTheoryProvider,
useBasisTheory,
} from '@basis-theory/basis-theory-react';
const App = () => {
const { bt } = useBasisTheory('<Public API Key>', {
elements: true,
});
return (
<BasisTheoryProvider bt={bt}>
<MyComponent />
</BasisTheoryProvider>
);
};
const MyComponent = () => {
// calling this hook with no attributes grabs the instance from Context
const { bt } = useBasisTheory();
return <div>My content</div>;
};
Elements capabilities are available when passing elements: true
in initialization options.
import {
BasisTheoryProvider,
TextElement,
useBasisTheory,
} from '@basis-theory/basis-theory-react';
const App = () => {
const { bt } = useBasisTheory('<Public API Key>', {
elements: true,
});
return (
<BasisTheoryProvider bt={bt}>
<MyComponent />
</BasisTheoryProvider>
);
};
const MyComponent = () => {
// calling this hook with no attributes grabs the instance from Context
const { bt } = useBasisTheory();
return <TextElement id="myInput" />;
};
The provided scripts with the SDK will check for all dependencies, build the solution and run all tests.
Run the following command from the root of the project:
make verify