Notistack: [Question/Feature] How to create a snackbar inside a service ?

Created on 1 Mar 2019  路  3Comments  路  Source: iamhosseindhv/notistack

I use Apollo with an error link written this way:

import { ErrorResponse } from 'apollo-link-error';
import { GraphQLError } from 'graphql';

export const errorHandler = ({ graphQLErrors, networkError }: ErrorResponse): void => {
  if (graphQLErrors) {
    graphQLErrors.map((error: GraphQLError) => {
      console.error(error);
    });
  }

  if (networkError) {
    console.error(`Network error: ${networkError.message}`);
  }
};

export default onError(errorHandler);

Is it possible to replace console.error by something like enqueueSnackbar ?

Most helpful comment

Hey @iamhosseindhv, I also have this use case (specifically from an API polling service and a PouchDB changes service). I've looked at #30's workaround, and it should work for me, but it would be great if there was a direct method.

If it's something that could go on the roadmap that would be amazing. Either way, thank you for your work on this library.

All 3 comments

@VincentLanglet notistack is not designed to work outside the component code. In my projects I tend to use something like the following:

fetchApi(url, method, payload)
    .then(() => {
           // ... enqueue snackbar
           // ... redirect to /dashboard page
    })
    .catch((err) => this.props.enqueueSnackbar(err, { variant: 'error' } ))

Or dispatch a enqueue action from Saga (if I'm using redux).

But there're other solutions presented here: #30
Namely this one: https://github.com/iamhosseindhv/notistack/issues/30#issuecomment-455822993

Hope this was useful.

Hey @iamhosseindhv, I also have this use case (specifically from an API polling service and a PouchDB changes service). I've looked at #30's workaround, and it should work for me, but it would be great if there was a direct method.

If it's something that could go on the roadmap that would be amazing. Either way, thank you for your work on this library.

I agree it'd be nice to have support for it.

Any implementation suggestion is welcome.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

usama-asfar picture usama-asfar  路  3Comments

ksavery picture ksavery  路  3Comments

RastislavMirek picture RastislavMirek  路  8Comments

seannguyn picture seannguyn  路  4Comments

nebojsanb picture nebojsanb  路  3Comments