Enhance Your Web Projects with Interactive UI Features

Are you looking to take your web projects to the next level by adding interactive UI features? In this article, we’ll explore how to implement spinners and notifications in a full-stack project, making it more user-friendly and engaging.

Getting Started

Before we dive in, let’s set up our project. We’ll be using an existing MERN project as a base, and you can find the GitHub repos for the server-side and client-side code here. Clone or download the repos, and make sure to create a MongoDB instance locally or on the cloud to store data. Follow the instructions in the README.md file to start the web app backend and client app’s development server.

Adding a GIF-Based Spinner Animation

First, let’s add a traditional GIF-based spinner animation to our project. We’ll create a Spinner.js file in the common folder and add a .gif file for the loading spinner. You can find many free GIF files online or generate your own using web-based tools. Update your Spinner.js file with the following code:

“`jsx
import spinner from ‘./spinner.gif’;

const Spinner = () => {
return Loading...;
};

export default Spinner;
“`

Next, update your ShowBookList.js file to add the loading spinner:

“`jsx
import React, { useState, useEffect } from ‘eact’;
import Spinner from ‘../common/Spinner’;

const ShowBookList = () => {
const [loading, setLoading] = useState(false);
const [listContent, setListContent] = useState([]);

useEffect(() => {
// Fetch data from API
setLoading(true);
//…
setLoading(false);
}, []);

return (

{loading? :

{listContent}

}

);
};

export default ShowBookList;
“`

Modern Spinners with React-Spinners

Now, let’s replace our GIF-based spinner with a modern CSS-animation-based spinner using the react-spinners package. First, install the package:


npm install react-spinners

Update your Spinner.js file with the following code:

“`jsx
import { ClipLoader } from ‘eact-spinners’;

const Spinner = () => {
return ;
};

export default Spinner;
“`

You can customize the spinner by adjusting the color, size, and other props.

Configuring Notifications with React-Notifications

Next, let’s handle notifications in our React project using react-notifications. First, install the package:


npm install react-notifications

Update your App.js file to set up the notification container:

“`jsx
import { NotificationContainer } from ‘eact-notifications’;

const App = () => {
return (


);
};
“`

Now, let’s pass notifications from different components to display their message. Update your CreateBook.js file to pass notifications:

“`jsx
import { NotificationManager } from ‘eact-notifications’;

const CreateBook = () => {
const handleCreateBook = () => {
// Create book logic
NotificationManager.success(‘Book created successfully!’);
};

return (

);
};
“`

Configuring Notifications with React-Toastify

Finally, let’s explore react-toastify for managing notifications. Install the package:


npm install react-toastify

Update your App.js file to set up the toast container:

“`jsx
import { ToastContainer } from ‘eact-toastify’;

const App = () => {
return (


);
};
“`

Now, let’s pass notifications through the toast container. Update your CreateBook.js file:

“`jsx
import { toast } from ‘eact-toastify’;

const CreateBook = () => {
const handleCreateBook = () => {
// Create book logic
toast.success(‘Book created successfully!’);
};

return (

);
};
“`

Conclusion

In this article, we’ve explored how to add interactive UI features to our web projects using spinners and notifications. We’ve discussed the benefits of using modern CSS-animation-based spinners over traditional GIFs and demonstrated how to configure notifications using react-notifications and react-toastify. By incorporating these features, you can enhance the user experience and make your web projects more engaging and interactive.

Leave a Reply

Your email address will not be published. Required fields are marked *