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

Nextjs 14 and context #245

Open
marcollilorenzo opened this issue Mar 11, 2024 · 1 comment
Open

Nextjs 14 and context #245

marcollilorenzo opened this issue Mar 11, 2024 · 1 comment

Comments

@marcollilorenzo
Copy link

I use Lucid with Nextjs 14

I have this context

'use client';

import {
  createContext,
  ReactNode,
  Suspense,
  useContext,
  useEffect,
  useState,
} from 'react';
import { Lucid } from 'lucid-cardano';

import { initLucid } from '@/lib/web3/cardano';

//types
interface LucidContextProps {
  children: ReactNode;
}

interface LucidContextInterface {
  lucid: Lucid | null;
}

// Constants
export type CARDANO_NETWORK = 'Mainnet' | 'Preview';
export const maestroKey = process.env.MAESTRO_KEY as string;
export const maestroNet = process.env.MAESTRO_NET as CARDANO_NETWORK;

// context init
export const LucidContext = createContext({} as LucidContextInterface);
export const useLucid = () => useContext(LucidContext);

// Provider
function LucidContextProvider({ children }: LucidContextProps) {
  // State
  const [lucid, setLucid] = useState<Lucid | null>(null);

  // Init
  const init = async () => {
    try {
      // Init lucid
      const lucidInstance = await initLucid();
      setLucid(lucidInstance);
    } catch (error) {
      console.error('LucidContextProvider', error);
    }
  };

  useEffect(() => {
    init();
  }, []);

  return (
    <LucidContext.Provider
      value={{
        lucid,
      }}
    >
      <Suspense fallback={<div>Loading...</div>}>{children}</Suspense>
    </LucidContext.Provider>
  );
}

export default LucidContextProvider;

And i import use di provider like this:

'use client';

import { lazy } from 'react';
import { ThemeProvider } from 'next-themes';

const LucidContextProvider = lazy(
  () => import('@/components/context/LucidContext')
);

export default function Providers({
  children,
}: Readonly<{ children: React.ReactNode }>) {
  return (

      <LucidContextProvider>

          <ThemeProvider
            attribute='class'
            defaultTheme='light'
            enableSystem
            disableTransitionOnChange
          >
            {children}
          </ThemeProvider>

      </LucidContextProvider>

  );
}

If i try to use const {lucid} = useLucid() i notice this error:

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

Check the render method of `StaticGenerationSearchParamsBailoutProvider`.
@dmurawsky
Copy link

dmurawsky commented Nov 2, 2024

bump! Getting this same error and only able to fix by removing dependancy on "lucid-cardano" package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants