Should a custom SnackbarHost use the Scaffold's default SnackbarHostState or a custom one?

47 Views Asked by At

There are apparently two ways to initialize a custom Snackbar.

You can do this by using a custom SnackbarHostState and ignoree the default one passed to the lambda. (I found it in this question while trying to make it work for the first time.)

// ignores default SnackbarHostState
val snackbarHostState = remember { SnackbarHostState() }
Scaffold(
    snackbarHost = { it: SnackbarHostState -> 
        SnackbarHost(
            hostState = snackbarHostState, // <-- uses custom SnackbarHostState
        ) { snackbarData -> }
    },
    topBar = { },
    bottomBar = { }
) { innerPadding -> }

But then I thought it's weird that the first solution ignores the default SnackbarHostState so I tried to use it in place of the custom one and it turned out, this works too!

val scaffoldState = rememberScaffoldState()
Scaffold(
    scaffoldState = scaffoldState,
    snackbarHost = { it: SnackbarHostState -> 
        SnackbarHost(
            hostState = it, // <-- uses default SnackbarHostState
        ) { snackbarData -> }
    },
    topBar = { },
    bottomBar = { }
) { innerPadding -> }

So now I'm wondering which one is correct? Are there any dis/advantages by using one or the other implementation?

0

There are 0 best solutions below