1.0.4 • Published 1 year ago

react-native-snackbar-hook v1.0.4

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

react-native-snackbar-hook

Screen Recording 2024-05-23 at 3 02 52 PM (2) info basic success basic success icon

Features :star_struck: :star_struck:

:sparkle: You can use useSnackBar hook to control the snackbar from any component.

:sparkle: SnackBarProvider higher order component helps to maintain clean code.

:sparkle: This package uses Animated API from react-native.

:sparkle: All the animations are running on the main thread.

:sparkle: No third-party packages are used.

:sparkle: Customize the SnackBar component the way you want. SnackBarProvideraccepts many properties to customize your SnackBar component.

Hey! Would you like to contribute to this package?! :raised_hands: :handshake:

Go ahead, fork the repo, and create many pull requests. :smile: :smile:

Installation Guide

install the package by running the below command

  npm install react-native-snackbar-hook

or

  yarn add react-native-snackbar-hook

How to use

Step 01

Wrap your screen component or the root component with SnackBarProvider

import { SnackBarProvider } from 'react-native-snackbar-hook';

<SnackBarProvider>
  <NavigationContainer>
    <Stacks/>
  </NavigationContainer>
</SnackBarProvider>

Step 02

import the useSnackBar and use bashowSnackBar and hideSnackBar functions. You need to pass the message and the type of SnackBar component you want to use to bashowSnackBar function.

import { useSnackBar } from "react-native-snackbar-hook";

export default function HomeScreen() {
  const { showSnackBar } = useSnackBar();

  const handlePress = () => {
    showSnackBar("This is an error message", "error");
  };

  return (
    <View style={styles.container}>
      <Button title="Show Error" onPress={handlePress} />
    </View>
  );
}

Props

SnackBarProvider Props

Prop nametyperequireddescriptiondefault value
globalOptionsSnackBarPropsNSet global properties. This will apply to all types of SnackBarnull
errorSnackBarPropsNSet properties for the error type SnackBarnull
successSnackBarPropsNSet properties for the success type SnackBarnull
infoSnackBarPropsNSet properties for the info type SnackBarnull

SnackBarProps

Prop nametyperequireddescriptiondefault value
colorstringNcolor of the SnackBarsuccess: #008c0c, info: #003f8c, error: #c20600
durationnumberNHow long the SnackBar should be visible3000 milliseconds
animationDurationnumberNHow long the animation should run300 miliseconds
autoHidebooleanNSnackBar will hide automatically after passing the duration timesuccess:true, info:true, error: false
showCloseButtonbooleanNshow predefined close button on the SnackBar. If you want add your own close button use closeButton property.success:false, info:false, error: true
iconReactNodeNAdd icon to the SnackBar. The icon will be placed before the message.null
textStyleTexyStyleNStyle the text in the SnackBarnull
closeButton(hideSnackBar?: () => void) => ReactNodeNAdd your own close button component here. But make sure to call hideSnackBar from your component to hide the SnackBar upon close button pressPredefined Component

useSnackBar return functions

Function namedescription
showSnackBarTo show the SnackBar component
hideSnackBarTo hide the SnackBar component

bashowSnackBar function arguments

Arg nametyperequireddescription
messagestringYtext that you want to show in the SnackBar
type"success" or "info" or "error"Ytype of the SnackBar