Alexandrebdry/react-mui-snackbar

Use an object instead of plain function arguments

Closed this issue · 1 comments

It would be great to have an object that would allow to target only one subset of the possible parameters that we can specify when calling the snackbar.

If someone wants to open the snackbar with a specific message it is easily done with this code.

openSnackbar("Hello, I'm under the water")

This works because the severity and duration arguments have default values. In this case, the success severity and 6000 duration is totally acceptable.

But if one wants to open the snackbar with only a message and a specific duration, without updating the severity, it would still imply writing down explicitely the severity by hand.

openSnackbar("Hello", "success", 2000)

This can lead to errors when a user does not pay enough attention writing a bad severity for instance.

openSnackbar("Hello", "sucess", 2000)

A solution would be to use an object for the argument of the openSnackbar instead.

openSnackbar({message: "Hello", severity: "success", duration: 2000})

Insteadn, this would be more verbose of course, but at the same time more explicit, it offers some capabilities like only specifying the duration and message for instance.

openSnackbar({message: "Hello", duration: 2000})

Reducing any mistake made when writing the severity when not necessary.

And this would totally work. This also helps reduce the cognitive wall that the first version leads to, making re-read the code even faster than before and understanding it better.

One huge drawback is that if this is implemented in a next version, this next version would be a major breaking change since it would break any existing code written in the previous format.

Well, I havn't think about it. It has been added ! :D