Skip to content

[React SDK] Expose a way to hook into LD Connection Errors #175

@martinmckenna

Description

@martinmckenna

The Problem

Currently, our app that relies on the LaunchDarkly service is render blocked by the actual LD Service. In other words, if the LD Service fails to connect, our app will never load.

Specifically if this line fails, we never know.

This is in a nutshell how we use the React SDK

import React from 'react'

export const App: React.FC<Props> = props => {
  const { userID } = props;

  /** PROBLEM: THIS MAY NEVER INIT AND BE UNDEFINED FOREVER */
  const client = useLDClient();

  const [isLoading, setLoading] = React.useState<boolean>(true)

  React.useEffect(() => {
    if (client && userID) {
      client
        .identify({
          key: userID
        })
        /**
         * set loaded to true if client fails or succeeds.
         * 
         * We don't want to render block if the flags can't be loaded.
         */
        .then(() => setLoading(false))
        .catch(() => setLoading(false));
    }
  }, [client, userID]);

  if (isLoading) {
    return null;
  }

  return <App />;
};

export default App;

but the issue here is that we have no way of knowing whether or not the connection succeeded.

Ideal Solution

Provide a new prop, maybe called ldConnectionError to the <WrappedComponent /> here..

So that could open a consumer component up to the following possibilities

import React from 'react'

export const App: React.FC<Props> = props => {
  const { userID } = props;

  const [client, clientError] = useLDClient();

  const [isLoading, setLoading] = React.useState<boolean>(true)

  React.useEffect(() => {
    if (client && userID) {
      client
        .identify({
          key: userID
        })
        /**
         * set loaded to true if client fails or succeeds.
         * 
         * We don't want to render block if the flags can't be loaded.
         */
        .then(() => setLoading(false))
        .catch(() => setLoading(false));
    }
  }, [client, userID, clientError]);

  if (isLoading && !clientError) {
    return null;
  }

  return <App />;
};

export default App;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions