React Web
Learn how to use the Lens React SDK with a web app.
Quick Start
Follow these steps to integrate the Lens React SDK into your web app.
This guide illustrates an integration using Wagmi v2 as the library for interacting with the connected wallet. If you're using a different wallet integration strategy, you can create custom bindings tailored to your needs.
Install the Lens Wagmi bindings package and its peer dependencies.
Follow the Wagmi documentation to create the Wagmi configuration.
Next, use this configuration with the bindings from the @lens-protocol/wagmi package to generate the LensConfig object.
To wrap up, here's an example with all the steps put together.
That's it—you're ready to use the Lens React SDK in your app.
Next.js
If you're planning to use Next.js, you can leverage the create-next-app CLI tool along with the lens-next-app template we've prepared. This template includes a Next.js project with the Lens React SDK and ConnectKit already integrated. To bootstrap your new Lens app, simply execute one of the commands below.
Additionally, you must set up the WalletConnect's Project ID in the .env file. You can quickly and easily create a free Project ID at WalletConnect Cloud.
Then follow the steps in the README file of the generated project to get started. Happy coding!
Additional Options
SDK Playground
You can play with the Lens React SDK in the Playground.
Custom Bindings
If you're not using Wagmi, you can create your own bindings by implementing the IBindings interface.
The example below demonstrates how to create custom bindings for a Wallet from ethers.js.
Debug Mode
The LensConfig object allows you to enable debug mode by setting the debug property to true. Doing so will:
Log additional information to the console for debugging purposes.
Disable gas estimation for self-funded transactions.
Enable Apollo Client Devtools, providing you with a detailed look at the operations of the integrated Apollo client.
App.tsx
const lensConfig: LensConfig = { // ... debug: true,};
React Suspense
The Lens React SDK offers experimental support for data fetching using React Suspense. This feature is being introduced gradually across all relevant hooks.
Hooks that support React Suspense include a suspense option. When this option is set to true, the hook suspends rendering until data is available. If an unexpected error occurs during this process, it will be thrown, allowing you to handle it using an error boundary.
Any known failure scenarios will be returned as part of the error object, which you can handle as part of your rendering logic.
The methodology described above can be applied to any of the following hooks:
Wagmi 1.x
If you are using Wagmi 1.x, follow the steps below.
First, install the wagmi-v1 tag of the @lens-protocol/wagmi package.
Then, create the LensConfig object like this.
Below a comprehensive example.