Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 5546

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 27, 20242024-11-27T10:16:08+00:00 2024-11-27T10:16:08+00:00

How to add a notification banner or popup to your website

  • 61k

A product's ability to capture the user's attention is crucial, and dynamic prompts/notification is one such efficient way to engage users.✨

It allows you to communicate with users by directing their attention to the navigation bar and can be used for a variety of purposes, such as for announcing business news, showing off the best features, generating leads, and so on.

This article will guide you step by step to build your own Dynamic Prompts/Notifications bar. 👩‍💻

We used React for the frontend, Canonic for the Part backend.

Let's begin! 📌

📝Step 1: Create-react-app

Lets start with creating a new react project – Use create-react-app

npx create-react-app dynamic-notifications 
Enter fullscreen mode Exit fullscreen mode

Next, to create a basic skeleton, just edit src/App.js

import React from "react"; import "./App.css";  function App() {   return (     <div className="app">       <section className="app-body">         <h3>Dynamic popups and notifications</h3>         <p>These notifications come from the data stored on your Canonic app</p>       </section>     </div>   ); }  export default App; 
Enter fullscreen mode Exit fullscreen mode

✨Step 2: Add some styling

Once you're done with the basic skeleton, it's time to add some styling to it. To add the styling use – Edit src/App.css

body {   margin: 0;   height: 100vh;   overflow: hidden;   font-family: sans-serif;   -webkit-font-smoothing: antialiased;   -moz-osx-font-smoothing: grayscale; }  h3 {   font-size: calc(10px + 2vmin);   margin-bottom: 8px; }  .app {   text-align: center; }  .app-body {   background-color: #020d57;   min-height: 100vh;   display: flex;   flex-direction: column;   align-items: center;   justify-content: center;   color: white;   overflow: hidden; }  .app-body p {   font-size: calc(2px + 2vmin);   margin: 0;   opacity: 0.5; } 
Enter fullscreen mode Exit fullscreen mode

After completing the steps above, run npm start and you should have something similar to this👇

step2

🔔Step 3: Add the Notification details

Now it's time to add the notification and some basic styles, add the HTML to src/App.js

... <div className="app">     **<header className="app-notification"> 🚀 This is a notification </header>**     <section className="app-body"> ... 
Enter fullscreen mode Exit fullscreen mode

✨Step 4: Make it look nice!

Let's add some styling to app, edit src/App.css

 ...  .app-notification {   position: absolute;   background-color: #4760ff;   padding: 8px;   width: 100%;   color: #fff; }  .app-notification p {   font-size: 14px;   font-weight: 600;   margin: 0; }  ... 
Enter fullscreen mode Exit fullscreen mode

Once done with the stated steps, this is how it should look: 👇

Step 4

👩‍🔧Step 5: Let's make it dynamic

Let's move ahead and make it dynamic.

Here, we want to show a different popup based on certain criteria. However, if we do it on the front end, the logic can easily be exposed. Thus, let’s rather create an API where the logic is computed on the backend, using Canonic.

Clone this project and head on to the CMS to create a few notifications.

Step 5

🚀Step 6: Create custom API

Once done, head on to the API section and create an API to return random notifications.

Step 6

Put the below code into the code section to return random notifications from the database.

module.exports = async function getRandom() {   const count = await Notification.count();   const random = Math.floor(Math.random() * count);   return Notification.findOne().skip(random); } 
Enter fullscreen mode Exit fullscreen mode

Step 6

🧪Step 7: Let's test it!

In order to do the testing, navigate to the Docs section and select the newly created endpoint. Click Open In Playground and hit the play button. The data should now appear.

Step 7

⚒Step 8: Time to integrate backend

We can easily integrate it by modifying App.js so that we fetch the data.

We'll use Axios to make the API call, install it using –npm install axios.

Make sure to replace "YOUR_API_URL_HERE" with the URL for your endpoint. You should be able to get that from the docs section.

... import axios from "axios" ... function App() {   const [notification, setNotification] = React.useState();    const getNotification = React.useCallback(     () =>       axios("YOUR_API_URL_HERE").then(({ data }) => data.data),     []   );    React.useEffect(() => {     getNotification().then(setNotification);   }, [getNotification, setNotification]);      ...      <div className="app">         {notification && (       <header         className="app-notification"         dangerouslySetInnerHTML={{ __html: notification.text }}       />     )}     ... 
Enter fullscreen mode Exit fullscreen mode

🔔Step 9: Refresh to get random notification

After the successful implementation of all the above states steps, you will get a random notification each time you refresh.

Step 9

Step 9

🎉 Voila!

You have successfully made your one dynamic prompt. 💃🕺

Check out the live demo here and sample code on github here.


Conclusion

Hope this guide helps you. You can also check out our other guides here.

Join us on discord to discuss or share with our community. Write to us for any support requests at support@canonic.dev. Check out our website to know more about Canonic.


beginnersreacttutorialwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.