Skip to content

Issue with React + Vite + SWC #270

Open
@brlocky

Description

@brlocky

Hi all, I started a new ReactJS project with Vite + SWC, but i'm having some weird issues, maybe someone can help me.
I have the exact same provider in an older react project and its working fine

So this is the error message on the console

websocket-client.ts:101 Uncaught TypeError: Class extends value undefined is not a constructor or null
    at node_modules/bybit-api/lib/websocket-client.js (websocket-client.ts:101:38)
    at __require (chunk-DFKQJ226.js?v=fadb9309:8:50)
    at node_modules/bybit-api/lib/index.js (index.ts:14:1)
    at __require (chunk-DFKQJ226.js?v=fadb9309:8:50)
    at index.ts:18:1

Currently testing with provider but already tested in the component, same result

import { RestClientV5 } from 'bybit-api';
import React, { ReactNode, useContext, useState } from 'react';

// Define the API context
const ApiContext = React.createContext<RestClientV5 | null>(null);

interface IApiProviderProps {
  children: ReactNode;
}

// Create the API provider component
export const ApiProvider: React.FC<IApiProviderProps> = ({ children }: IApiProviderProps) => {
  const [apiClient, setApiClient] = useState<RestClientV5 | null>(null);

  // Initialize the API client once
  if (!apiClient) {
    // Retrieve the API key and API secret from SettingsService
    const client = new RestClientV5({ key: 'xxxxx', secret: 'xxxxxx', testnet: true });
    setApiClient(client);
  }

  return <ApiContext.Provider value={apiClient}>{children}</ApiContext.Provider>;
};

// Create a custom hook to access the API context
export const useApi = () => {
  const apiClient = useContext(ApiContext);

  if (!apiClient) {
    throw new Error('useApi must be used within an ApiProvider');
  }

  return apiClient;
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is neededquestionGeneral question / more info needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions