iamhosseindhv / notistack

Highly customizable notification snackbars (toasts) that can be stacked on top of each other
https://notistack.com
Other
3.9k stars 299 forks source link

useless bind of arrow function #603

Open jonenst opened 4 months ago

jonenst commented 4 months ago

Expected Behavior

SnackbarProvider code is simple

Current Behavior

SnackbarProvider code works but has useless bind or contextValue enqueueSnackbar I stumbled on this while browsing the code https://github.com/iamhosseindhv/notistack/blob/master/src/SnackbarProvider/SnackbarProvider.tsx#L51

export let enqueueSnackbar;

class SnackbarProvider extends Component<SnackbarProviderProps, State> {
    constructor(props: SnackbarProviderProps) {
        enqueueSnackbar = this.enqueueSnackbar;
        this.state = {
            contextValue: {
                enqueueSnackbar: this.enqueueSnackbar.bind(this),
        }}
    enqueueSnackbar = () => { ... }
}

since enqueueSnackbar is defined as an arrow function, you can't bind. It already has "this" to the instance of the class. That's why the global variable works without bind. the context value should be just se to enqueueSnackbar without binding. If I understood everything correctly.