0

I am trying to use the material UI snackbar to show pop up errors in my react application.

I am using a container view. In that view, it does some stuff and errors can be thrown. If it does get an error, I want to render my custom snackbar component.

This is my ErrorPopup component:

import React from 'react';
import { Snackbar } from '@material-ui/core';
import MuiAlert, { AlertProps } from '@material-ui/lab/Alert';

function Alert(props: AlertProps) {
  return <MuiAlert elevation={6} variant="filled" {...props} />;
}

interface ErrorProps {
  message: string;
}

export default function ErrorPopup(props: ErrorProps) {
  const [open, setOpen] = React.useState(true);

  const handleClose = () => {
    setOpen(false);
  };

  return (
    <div>
      {props.message !== '' ? (
        <Snackbar open={open} autoHideDuration={6000} onClose={handleClose}>
          <Alert onClose={handleClose} color="error">
            {props.message}
          </Alert>
        </Snackbar>
      ) : (
        ''
      )}
    </div>
  );
}

In my main view, I call this component like so:

<ErrorPopup message={this.state.errors} />

What seems to happen is I get errors that seem to me to indicate something about the WithStyles + typescript issue crops up, but I am out of my depth to fully understand what is going on. I just expected it to work as all my material UI stuff has worked up until now.

I have tried a couple of quick cut n paste run n gun type fixes off the net (as you do), but I clearly don't know what exactly is going on, so I need to at least start there.

Here is a screen grab:

With styles errors perhaps?

  1. First off, is this approach to showing the errors ok?
  2. Secondly, can anyone point me in the right direction here?
hellaeon
  • 33
  • 1
  • 8

1 Answers1

0

Frustratingly, I just ran an npm update on my project, and things started to work. For anyones reference, my dependencies in package.json

    "dependencies": {
        "@material-ui/core": "^4.8.3",
        "@material-ui/icons": "^4.5.1",
        "@material-ui/lab": "^4.0.0-alpha.39",
        "@types/pouchdb": "^6.4.0",
        "@types/react-router-dom": "^5.1.3",
        "clsx": "^1.0.4",
        "highcharts": "^7.2.1",
        "highcharts-react-official": "^2.2.2",
        "pouchdb": "^7.1.1",
        "pouchdb-find": "^7.1.1",
        "pubnub": "^4.27.3",
        "react": "^16.12.0",
        "react-dom": "^16.12.0",
        "react-router-dom": "^5.1.2",
        "react-scripts": "^3.3.0",
        "request": "^2.88.0",
        "typescript": "^3.7.4"
    },

Whilst this solves my original problem getting it working, is this the right approach?

Cheers

hellaeon
  • 33
  • 1
  • 8