web3-react 错误:Web3ReactProvider 未调用 getLibrary 函数
我在一个简单的反应应用程序上遇到这个问题。我正在尝试使用“连接”按钮连接到元掩码,但是当我单击该按钮时,控制台显示错误“错误:不变失败:未找到
正如您所看到的,我向 getLibrary 函数添加了一个控制台,但它没有记录日志,因此,该函数永远不会被调用。
import "./App.css";
import React from "react";
import { Web3ReactProvider, useWeb3React } from "@web3-react/core";
import { Web3Provider } from "@ethersproject/providers";
import { InjectedConnector } from "@web3-react/injected-connector";
const injected = new InjectedConnector({
supportedChainIds: [1, 3, 4, 5, 42],
});
function getLibrary(provider) {
console.log("getting library")
const library = new Web3Provider(provider)
library.pollingInterval = 12000
return library
}
const App = () => {
const { active, account, library, connector, activate, deactivate } =
useWeb3React();
async function connect() {
console.log("connecting");
try {
await activate(injected);
} catch (ex) {
console.log(ex);
}
}
async function disconnect() {
try {
deactivate();
} catch (ex) {
console.log(ex);
}
}
return (
<Web3ReactProvider getLibrary={getLibrary}>
<div className="App">
{!active ? <button onClick={connect}>Connect</button> : <button onClick={connect}>Disconnect</button>}
</div>
</Web3ReactProvider>
);
}
I have this issue on a simple react app. I am trying to connect to metamask using a "Connect" button, but when i click that button the console shows an error saying "Error: Invariant failed: No <Web3ReactProvider ... /> found."
As you can see, I added a console to the getLibrary function and it is not logging, so, that function is never called.
import "./App.css";
import React from "react";
import { Web3ReactProvider, useWeb3React } from "@web3-react/core";
import { Web3Provider } from "@ethersproject/providers";
import { InjectedConnector } from "@web3-react/injected-connector";
const injected = new InjectedConnector({
supportedChainIds: [1, 3, 4, 5, 42],
});
function getLibrary(provider) {
console.log("getting library")
const library = new Web3Provider(provider)
library.pollingInterval = 12000
return library
}
const App = () => {
const { active, account, library, connector, activate, deactivate } =
useWeb3React();
async function connect() {
console.log("connecting");
try {
await activate(injected);
} catch (ex) {
console.log(ex);
}
}
async function disconnect() {
try {
deactivate();
} catch (ex) {
console.log(ex);
}
}
return (
<Web3ReactProvider getLibrary={getLibrary}>
<div className="App">
{!active ? <button onClick={connect}>Connect</button> : <button onClick={connect}>Disconnect</button>}
</div>
</Web3ReactProvider>
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题是您试图在定义上下文提供程序的同一组件中调用 use 挂钩。将 Provider 移至更高一级,进入调用
App
的组件,或者将钩子和剩余逻辑移至更低一级的新组件中。The issue here is that you are trying to call the use hook in the same component where you define the Context Provider. Move the Provider one level higher, into the component calling
App
, or move the hook and remaining logic one level lower into a new component.