useDAppuseDApp
  • 开始
  • 指南
  • 代币
⌘ K
安装
读取
写入
Create Eth App
最后更新时间:
帮助改进此文档
Made with ❤️ by 紫升

安装

要开始使用 useDApp,您需要拥有一个可用的 React 环境。

要开始,请在您的项目中添加以下 npm 包 @usedapp/core 及其对等依赖项。

# Yarn
$ yarn add @usedapp/core ethers@5.7.2
# Or NPM
$ npm install @usedapp/core ethers@5.7.2

示例

下面是一个简单的例子:

设置

你需要做的第一件事是使用可选配置设置 DAppProvider 并将整个应用程序包装在其中。

<DAppProvider>
<App /> {/* 使用 Provider 包装你的应用程序。 */}
</DAppProvider>

连接到浏览器钱包

然后,您需要使用 activateBrowserWallet 激活提供程序。最好在用户单击“连接”按钮时执行此操作。

const App = () => {
const { activateBrowserWallet, account } = useEthers()
return (
<div>
<div>
<button onClick={() => activateBrowserWallet()}>Connect</button>
</div>
{account && <p>Account: {account}</p>}
</div>
)
}

激活后(即用户连接到像 MetaMask 这样的钱包),该组件将显示用户的地址。

如果你需要使用除浏览器钱包之外的其他连接器,请使用 useEthers 中的 activate 方法。请参阅 web3-react 文档了解更多信息。