API

December 15, 2023 ยท View on GitHub

The Toast API consists of:

  1. methods that can be called directly on the Toast object (in an imperative way)
  2. props that can be passed to the Toast component instance; they act as defaults for all Toasts that are shown

methods

show(options = {})

To show a Toast, call the show() method and pass the options that suit your needs. Everything is optional, unless specified otherwise:

import Toast from 'react-native-toast-message'

Toast.show({
  type: 'info',
  text1: 'This is an info message'
});

The complete set of options is described below:

optiondescriptiontypedefault value
typeToast type. Default available values: success, error, info. Learn how to extend / overwrite Toast typesstringsuccess
text1First line of textstring
text2Second line of textstring
positionToast positiontop or bottomtop
visibilityTimeNumber of milliseconds after which Toast automatically hides. Has effect only in conjunction with autoHide prop set to truenumber4000
autoHideWhen true, the visible Toast automatically hides after a certain number of milliseconds, specified by the visibilityTime propbooleantrue
topOffsetOffset from the top of the screen (in px). Has effect only when position is topnumber40
bottomOffsetOffset from the bottom of the screen (in px). Has effect only when position is bottomnumber40
keyboardOffsetOffset from the Keyboard (in px). Has effect only when position is bottom and Keyboard is visible (iOS only)number10
onShowCalled when the Toast is shown() => void
onHideCalled when the Toast hides() => void
onPressCalled on Toast press() => void
propsAny custom props passed to the specified Toast type. Has effect only when there is a custom Toast type (configured via the config prop on the Toast instance) that uses the props parameterany

hide()

To hide the current visible Toast, call the hide() method:

Toast.hide();

If an onHide callback was set (via show(), or as a default prop on the Toast component instance), it will be called now.

props

The following set of props can be passed to the Toast component instance to specify certain defaults for all Toasts that are shown:

propdescriptiontypedefault value
configLayout configuration for custom Toast typesToastConfig
typeDefault Toast typestringsuccess
positionDefault Toast positiontop or bottomtop
visibilityTimeNumber of milliseconds after which Toast automatically hides. Has effect only in conjunction with autoHide prop set to truenumber4000
autoHideWhen true, the visible Toast automatically hides after a certain number of milliseconds, specified by the visibilityTime propbooleantrue
swipeableWhen true, the Toast can be swiped to dismissbooleantrue
topOffsetOffset from the top of the screen (in px). Has effect only when position is topnumber40
bottomOffsetOffset from the bottom of the screen (in px). Has effect only when position is bottomnumber40
keyboardOffsetOffset from the Keyboard (in px). Has effect only when position is bottom and Keyboard is visible (iOS only)number10
onShowCalled when any Toast is shown() => void
onHideCalled when any Toast hides() => void
onPressCalled on any Toast press() => void

For example, to make sure all your Toasts are displayed at the bottom of the screen:

// App.jsx
import Toast from 'react-native-toast-message';

export function App(props) {
  return (
    <>
      {/* ... */}
      <Toast
        position='bottom'
        bottomOffset={20}
      />
    </>
  );
}