Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getLibrary does not get called #126

Closed
ArinCantCode opened this issue Sep 30, 2020 · 5 comments
Closed

getLibrary does not get called #126

ArinCantCode opened this issue Sep 30, 2020 · 5 comments
Labels
bug Something isn't working

Comments

@ArinCantCode
Copy link

ArinCantCode commented Sep 30, 2020

Bug Description
Provided callback for getLibrary does not get triggered

My main component

 <Web3ReactProvider getLibrary={getLibrary}>
      <AppContainer>
        <Row>
          <Col col={12}>
            <PurchasesCarousel purchasesCarousel={me.purchasesCarousel} />
          </Col>
          <Media greaterThanOrEqual="lg">
            {mediaClassNames => {
              return (
                <PurchaseRoadWrapper className={mediaClassNames}>
                  <PurchaseRoad purchaseRoad={me.purchaseRoad} />
                </PurchaseRoadWrapper>
              )
            }}
          </Media>
          <BarWrapper col={true} order={[-1, -1, -1, 0]}>
            <SalePointsBar salePointsBar={me.salePointsBar} />
            <BenefitsBar benefitsBar={me.benefitsBar} />
          </BarWrapper>
        </Row>
      </AppContainer>
    </Web3ReactProvider>

getLibrary

function getLibrary(provider: any): Web3Provider {
  console.log("test")
  const library = new Web3Provider(provider)
  library.pollingInterval = 12000
  return library
}

Expected Behavior

see the log "test"

Current Behavior
My app renders but the getLibrary is never executed

Additional Context
I'm working with graphql and relay / storybooks so dunno if this setup breaks anything

@ArinCantCode ArinCantCode added the bug Something isn't working label Sep 30, 2020
@johnwildspring
Copy link

@ArinCantCode I am having this same issue. What did you do to fix?

@kuanlim990
Copy link

Fh

@Ucodia
Copy link

Ucodia commented Feb 22, 2021

@ArinCantCode I ran into the same issue. I found that you also need to call the activate function with the connector of your choice. Here is an example that uses the browser extension provider (injected connector).

import React from "react";
import ReactDOM from "react-dom";
import { Web3ReactProvider, useWeb3React } from "@web3-react/core";
import { InjectedConnector } from "@web3-react/injected-connector";
import { Web3Provider } from "@ethersproject/providers";

const injected = new InjectedConnector({
  supportedChainIds: [1, 3, 4, 5, 42],
});

const getLibrary = (provider, connector) => {
  const library = new Web3Provider(provider);
  library.pollingInterval = 12000;
  return library;
};

const App = () => {
  const web3 = useWeb3React();

  const handleConnect = () => {
    try {
      web3.activate(injected, undefined, true);
    } catch (error) {
      console.error(error);
    }
  };

  return (
    <div>
      <button onClick={handleConnect}>Connect</button>
      <p>
        <span>Status: {web3.active ? "🟢" : web3.error ? "🔴" : "🟠"}</span>
      </p>
      <pre>{JSON.stringify(web3, null, 2)}</pre>
    </div>
  );
};

ReactDOM.render(
  <React.StrictMode>
    <Web3ReactProvider getLibrary={getLibrary}>
      <App />
    </Web3ReactProvider>
  </React.StrictMode>,
  document.getElementById("root")
);

@PfanP
Copy link

PfanP commented Feb 24, 2021

Same issue

@nrgapple
Copy link

nrgapple commented Mar 3, 2021

@ArinCantCode I ran into the same issue. I found that you also need to call the activate function with the connector of your choice. Here is an example that uses the browser extension provider (injected connector).

import React from "react";
import ReactDOM from "react-dom";
import { Web3ReactProvider, useWeb3React } from "@web3-react/core";
import { InjectedConnector } from "@web3-react/injected-connector";
import { Web3Provider } from "@ethersproject/providers";

const injected = new InjectedConnector({
  supportedChainIds: [1, 3, 4, 5, 42],
});

const getLibrary = (provider, connector) => {
  const library = new Web3Provider(provider);
  library.pollingInterval = 12000;
  return library;
};

const App = () => {
  const web3 = useWeb3React();

  const handleConnect = () => {
    try {
      web3.activate(injected, undefined, true);
    } catch (error) {
      console.error(error);
    }
  };

  return (
    <div>
      <button onClick={handleConnect}>Connect</button>
      <p>
        <span>Status: {web3.active ? "🟢" : web3.error ? "🔴" : "🟠"}</span>
      </p>
      <pre>{JSON.stringify(web3, null, 2)}</pre>
    </div>
  );
};

ReactDOM.render(
  <React.StrictMode>
    <Web3ReactProvider getLibrary={getLibrary}>
      <App />
    </Web3ReactProvider>
  </React.StrictMode>,
  document.getElementById("root")
);

Thanks for posting something that actually gives an example of a Web3Provider.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants