mdtanrikulu / use-metamask

a custom React Hook to manage Metamask in Ethereum ĐApp projects
https://mdtanrikulu.github.io/use-metamask
MIT License
165 stars 35 forks source link

Integration with NextJS #7

Closed damianlluch closed 3 years ago

damianlluch commented 3 years ago

Hello, this project seems to be very good, do you know how I could integrate this library in a project with Next?

Thanks

damianlluch commented 3 years ago

I tried with this library in an project with Next. And I get this error:

TypeError: dispatch is not a function

image

I reproduced only the example from de main page.

Any suggestions?

thanks!

mdtanrikulu commented 3 years ago

Heya! Thanks for the issue. I tried to reproduce the error with nextjs create app template but failed.

image



Could you give me more details? If you press useMetamask.js?cdc:138 url in second line of the error you got, this may route you to code block causing the issue.

damianlluch commented 3 years ago

Heya! Thanks for the issue. I tried to reproduce the error with nextjs create app template but failed.

image

Could you give me more details? If you press useMetamask.js?cdc:138 url in second line of the error you got, this may route you to code block causing the issue.

perfect!, you can share this example?

mdtanrikulu commented 3 years ago

sure, here you go. but please let me know the cause of the code line by following the instruction from my previous message. so I can see what I can improve.

nextjsapp.zip

damianlluch commented 3 years ago

Perfect, now I'll look at the code you sent, and I'll let you know.

damianlluch commented 3 years ago

I have the issue.

image

image

mdtanrikulu commented 3 years ago

Oh, I guess I know the issue now. Did you wrap your root component with <MetamaskStateProvider> ? As shown here in first step.

Your root component must look like this;

import React                     from 'react';
import ReactDOM                  from 'react-dom';
import { MetamaskStateProvider } from "use-metamask";
import App                       from './App';

ReactDOM.render(
    <MetamaskStateProvider>
      <App />
    </MetamaskStateProvider>
  document.getElementById('root')
);

You can also see _app.js file that I shared as nextjs example with you.

damianlluch commented 3 years ago

Hi, yes I have wrapped it over pages/_app.js

mdtanrikulu commented 3 years ago

Hi, yes I have wrapped it over pages/_app.js

That's interesting. Is there any chance I can see your implementation if it's open source?

The only way I was able to reproduce your issue, when I don't use MetamaskStateProvider wrapper, thus;

  const dispatch = useContext(MetaDispatchContext);

in my hook implementation returns undefined. The component you call useMetamask hook must be child or descendant of this wrapped root component. Other than that I could not find any other case this can happen.

mdtanrikulu commented 3 years ago

Hey @damianlluch! Is it still a problem on your side? Is there anything I can help there?

mdtanrikulu commented 3 years ago

Closing this issue due to inactivity, feel free to open if anything related this topic arise.