Hi there! I'm a full-stack developer who loves crafting web and mobile apps. When I'm not coding, I enjoy sharing my knowledge as a technical writer. Let's build something awesome together!
In today's tutorial, we will implementing custom animated toast messages in React Native. These toast messages are perfect for displaying in-app notifications.
Let's kick things off by creating a new project. Open your terminal and run the following command:
For this project, we will use the React Native Reanimated library to add animated fade-in and fade-out effects to our components. Install it with the following command:
Don't forget to update your babel.config.js file as well:
This component is a bit more complex, so let's break it down. We designed the UI using View, Text, and FontAwesome5 icons.
To achieve the desired animation, we used the Animated.View component and defined the animation behavior with entering and exiting properties.
We will call the showToast method from the parent component, so we need a reference to the object. That's why we define the component with forwardRef and implement the useImperativeHandle method. Inside this method, you'll find the showToast function, which called from parent.
When the showToast method is invoked, it starts a timer and makes the component visible by setting setIsVisible to true.
This component has five properties:
type: Determines the background color and icon of the component, with options including success, danger, info, and warning.
text: Specifies the text field of the component.
description: This property sets the description field of the component.
** timeout:** Specifies how many milliseconds the toast notification will remain visible.
ref: The ref object used to call the showToast function.
Finally, we import and use the Button and ToastMessage components. When the button is pressed, the handleShowToast function is called, which in turn invokes the ToastMessage component using a ref.
Just update the type property of ToastMessage and Button components. For example;
If you want to see all type of Buttons and ToastMessages, change the code like this and enjoy!
In conclusion, you now have the information and code necessary to implement customized animated toast notificaton messages into your React Native applications. By providing attention-grabbing in-app alerts, you can quickly improve your user experience with the Button and ToastMessage components.