<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pratik Bhagat</title>
    <description>The latest articles on DEV Community by Pratik Bhagat (@bhagatpratik07).</description>
    <link>https://dev.to/bhagatpratik07</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F663641%2Fd5184d0f-69bf-4a5e-99ff-3cf97e066508.png</url>
      <title>DEV Community: Pratik Bhagat</title>
      <link>https://dev.to/bhagatpratik07</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bhagatpratik07"/>
    <language>en</language>
    <item>
      <title>My Journey of Winning Smart India Hackathon 2022: Lessons and Tips for SIH 2023</title>
      <dc:creator>Pratik Bhagat</dc:creator>
      <pubDate>Wed, 30 Aug 2023 11:33:36 +0000</pubDate>
      <link>https://dev.to/bhagatpratik07/my-journey-of-winning-smart-india-hackathon-2022-lessons-and-tips-for-sih-2023-5ajc</link>
      <guid>https://dev.to/bhagatpratik07/my-journey-of-winning-smart-india-hackathon-2022-lessons-and-tips-for-sih-2023-5ajc</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Hey there, awesome folks!&lt;/p&gt;

&lt;p&gt;I'm Pratik, and guess what? I had a blast winning the Smart India Hackathon 2022. 🎉 But you know what's even more exciting? The fact that the SIH 2023 excitement is in the air, and so many of you have been sliding into my DMs for a slice of advice. Well, you asked, and I'm delivering – in the form of this blog post!&lt;/p&gt;

&lt;p&gt;So, whether you're a coding newbie or a hackathon pro, let's embark on a journey together. I'll spill the beans on how my team and I won SIH, and I've got some battle-tested tips up my sleeve to help you in the 2023 edition. Ready to dive in?&lt;/p&gt;

&lt;p&gt;Keep scrolling, and let's hack our way to success!&lt;/p&gt;

&lt;h2&gt;
  
  
  The Adventure Begins: Understanding SIH
&lt;/h2&gt;

&lt;p&gt;The Smart India Hackathon (SIH) isn't just an event; it's a journey that starts right at your college campus and takes you on a nationwide innovation roller coaster. So, let's break it down step by step:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Internal Hackathons&lt;/em&gt;&lt;/strong&gt;: It all begins within the cozy confines of your college. The SIH journey starts with internal hackathons, which can vary in structure from college to college. These hackathons are like the starting point for your SIH experience.  The college SPOC will be responsible for nominating the top 35 teams as per the internal hackathon results.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Connecting with Organizations&lt;/em&gt;&lt;/strong&gt;: The real fun begins as your journey progresses to the next stage. The problem statements, the very heart of SIH, are sent to their respective organizations. This is where your innovation meets real-world challenges.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The Finale Beckons&lt;/em&gt;&lt;/strong&gt;: Out of the numerous national teams, only around 4 to 5 teams per problem statement are chosen. These selected teams become the crème de la crème, the contenders for the grand finale. And here's the exciting part—the finale isn't virtual. It's the time when your digital journey converges into a real-world gathering. The grand finale is held offline at various nodal centres scattered across India&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Gearing Up for the Grand Finale&lt;/em&gt;&lt;/strong&gt;: The teams gather, the excitement is palpable, and the energy is infectious. Your journey culminates in these grand finales, where you present your solution in person, face to face with the organizations that posed the challenges.&lt;/p&gt;

&lt;h2&gt;
  
  
  Picking the Problem Statement
&lt;/h2&gt;

&lt;p&gt;Alright, let's spill some beans on a little secret to SIH success: finding the problem statement that clicks with your team's skills and interests is like nailing the first level of a game. It's crucial. Think about it - you wouldn't send a wizard to fight dragons without a wand, right?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Playing to Our Strengths:&lt;/em&gt;&lt;/strong&gt; So, what did we do? Our chosen problem statement was a true masterpiece: "Anti-doping awareness cum sensitization through Gaming App." Ah, the world of sports, gaming, and drugs - a match made in... well, somewhere quite amusing. Who would've thought that we'd find ourselves fighting the villain of doping through the power of gaming? Now, don't get us wrong. We might not be the poster children for athleticism, but we did have a genuine interest in sports, gaming and drugs(maybe). And sure, the whole anti-doping angle sounded pretty darn important too.&lt;/p&gt;

&lt;p&gt;So, the takeaway here? When picking your problem statement, make sure it's got a wink and a nod to your team's expertise and interests. It might not be the typical superhero backstory, but hey, it's yours. Own it, embrace it, and get ready to game-change your way through SIH!&lt;/p&gt;

&lt;h2&gt;
  
  
  Development Process
&lt;/h2&gt;

&lt;p&gt;Alright, you've got your problem statement locked in. Now, let the coding games begin! This is where the magic happens. The golden rule? Solve, solve, solve. Dive into the problem statement into parts/sections. Understand every nook and cranny of the problem. Read it, re-read it, and then read it again. The key is to know the ins and outs of the problem statement. Think of the problem statement as your blueprint. You're the architect who takes the blueprint and transforms it into a masterpiece. Whether you're coding your way through or building using a no-code tool, the goal remains the same: solve the problem. Here's the best part: you're not tied to a single path. You're free to pick the development tools, languages, or approaches that resonate with you. It's all about playing to your strengths and using your coding superpowers to overcome the challenge.&lt;/p&gt;

&lt;h2&gt;
  
  
  Presentation and Pitch
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Breaking it Down:&lt;/em&gt;&lt;/strong&gt; Imagine telling a story, but instead of bedtime tales, you're talking about your smart idea. Your goal? Make it easy for everyone – from the judges to regular folks – to understand what you're up to.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Process&lt;/em&gt;&lt;/strong&gt;: Your presentation journey kicks off with the training round, your first encounter with the evaluators. Remember, first impressions can leave a lasting mark. This round is more than just a "hello." It's your chance to show that you've grasped the problem statement and are all set to tackle it head-on over the next 36 hours. Consider this round as the place where you set the stage for your entire presentation journey. The evaluators begin forming an opinion right from here. So, make sure they understand your solution and the path you're about to tread. &lt;/p&gt;

&lt;p&gt;Now, it's time for the real journey – three evaluation rounds await you after the training. These rounds are your proving grounds. The judges take a closer look at your solution, how it evolves, and how you navigate challenges.&lt;/p&gt;

&lt;p&gt;Now, for a twist. Throughout the journey, keep your radar tuned to the evaluators' feedback. Surprising, right? But here's the thing: the judges' suggestions for modifications hold higher importance than even the problem statement itself. Yes, you read that right. Evaluators see potential beyond the initial pitch; they see adaptability.&lt;/p&gt;

&lt;p&gt;After the evaluation rounds, it's the moment you've been working towards – the final demo. Imagine standing in front of a panel of experts, ready to showcase your solution. It's your time to shine.&lt;/p&gt;

&lt;h2&gt;
  
  
  Winning Moment
&lt;/h2&gt;

&lt;p&gt;The winning moment at the Smart India Hackathon. It's when all your hard work, late nights, and brainpower pay off in the most incredible way. You took an idea, crafted it into a solution, and presented it like a pro. Winning means your creation stood out among the rest. It's like turning a dream into reality.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuow3m5tpa45mskgytzdh.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuow3m5tpa45mskgytzdh.gif" alt="Winning moment" width="600" height="339"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Even if the top spot doesn't come your way, don't let that dim your spirit. The Smart India Hackathon is more than a trophy; it's an experience of a lifetime. It's about learning, growing, and enjoying the journey. Win or not, the memories you've created during this journey are yours to keep forever. The friends you've made, the skills you've honed – they're all part of your victory story.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advice for SIH 2023 Participants
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Choose Passion Over Trend:&lt;/strong&gt; Opt for a problem statement that aligns with your passion, not just the trend. Your enthusiasm will fuel your journey.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Embrace Collaboration:&lt;/strong&gt; Hackathons are team efforts. Embrace diverse skill sets, communicate openly, and watch how collaboration elevates your solution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Innovate Fearlessly:&lt;/strong&gt; Don't be afraid to think outside the box. Innovation often springs from unconventional ideas. Dare to be different.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Practice, Practice, Practice:&lt;/strong&gt; Presentation skills matter. Rehearse your pitch till it's smooth. Engage your audience, and make your solution relatable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Enjoy Every Step:&lt;/strong&gt; The journey matters as much as the destination. Relish every moment, from ideation to presentation. It's a once-in-a-lifetime experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;As SIH 2023 approaches, embrace the challenges, collaborate with your team, and relish every step of the process. The friendships, lessons, and solutions you create will stay with you far beyond the final pitch. So, go ahead, code your dreams, and make your mark in the world of innovation.&lt;/p&gt;

&lt;p&gt;Here's to your journey and the impact you're about to make! Good luck! 🚀👩‍💻👨‍💻&lt;/p&gt;

&lt;p&gt;Drop a like if you found this post helpful :) Have questions? Feel free to DM me on &lt;a href="https://twitter.com/bhagatpratik07" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>hackathon</category>
      <category>learning</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Passwordless Authentication with WebAuthn</title>
      <dc:creator>Pratik Bhagat</dc:creator>
      <pubDate>Thu, 24 Nov 2022 19:21:16 +0000</pubDate>
      <link>https://dev.to/bhagatpratik07/passwordless-authentication-with-webauthn-26p8</link>
      <guid>https://dev.to/bhagatpratik07/passwordless-authentication-with-webauthn-26p8</guid>
      <description>&lt;p&gt;Imagine being able to log in to websites without using any password, sounds cool right?&lt;/p&gt;

&lt;p&gt;Passwords are hard to keep track of leading us to pick weak and easy ones which make us vulnerable to phishing attacks. WebAuthn solves this problem for us with passwordless authentication.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is WebAuthn?
&lt;/h2&gt;

&lt;p&gt;WebAuthn—short for Web Authentication is part of the &lt;a href="https://fidoalliance.org/fido2/" rel="noopener noreferrer"&gt;FIDO2&lt;/a&gt; framework, which is a set of technologies that enable passwordless authentication between servers, browsers, and authenticators.&lt;/p&gt;

&lt;p&gt;WebAuthn let users authenticate with two types of authenticators:&lt;/p&gt;

&lt;p&gt;One is with a &lt;strong&gt;Roaming authenticator&lt;/strong&gt; a physical device like a Yubikey which can be used on multiple devices. The second method is with &lt;strong&gt;Platform authenticators&lt;/strong&gt; attached to a device for example MacBook’s TouchBar, and Android’s fingerprint recognition.&lt;/p&gt;

&lt;h2&gt;
  
  
  How WebAuthn Works?
&lt;/h2&gt;

&lt;p&gt;It builds a trusted relationship between the device and the browser by creating public/private key pairs so they can communicate with each other and authenticate the user. This way, as a user, you don't need to have passwords for every site you visit, just a strong authenticator that works with WebAuthn.&lt;/p&gt;

&lt;p&gt;The public/private key is different for each website, so one website cannot determine from the authenticator what other websites you have used it for.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;No website supports WebAuthn...Wait!? What!?&lt;/p&gt;

&lt;p&gt;The thing is all major web applications support WebAuthn as a second-factor authentication so it's not entirely passwordless after all.&lt;/p&gt;

&lt;p&gt;The problem behind this is inconsistency in user experience between different browsers. Every website has different ways to set up passwordless authentication which makes it confusing and hard to understand for users.&lt;/p&gt;

&lt;p&gt;We can hope for complete passwordless authentication in future with WebAuthn as it offers user-friendly authentication.&lt;/p&gt;

&lt;h3&gt;
  
  
  Read more about WebAuthn
&lt;/h3&gt;

&lt;h3&gt;
  
  
  &lt;a href="//webauthn.me"&gt;webauthn.me&lt;/a&gt;
&lt;/h3&gt;

&lt;h3&gt;
  
  
  &lt;a href="//webauthn.guide"&gt;webauthn.guide&lt;/a&gt;
&lt;/h3&gt;

</description>
      <category>tooling</category>
    </item>
    <item>
      <title>Add authentication to your React apps in minutes with Auth0</title>
      <dc:creator>Pratik Bhagat</dc:creator>
      <pubDate>Tue, 30 Aug 2022 10:56:10 +0000</pubDate>
      <link>https://dev.to/bhagatpratik07/add-authentication-to-your-react-apps-in-minutes-5g0</link>
      <guid>https://dev.to/bhagatpratik07/add-authentication-to-your-react-apps-in-minutes-5g0</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;This blog will teach you how to add authentication to your React applications with Auth0.&lt;/p&gt;

&lt;p&gt;Auth0 is a flexible, drop-in solution to add authentication and authorization services to your applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up the application
&lt;/h2&gt;

&lt;p&gt;Create a new react application with the command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app app-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Head to the Auth0 website Sign up for a free Auth0 account and create a new Single Page Web Application and select React as the technology you will use.&lt;/p&gt;

&lt;p&gt;Next, go to the settings of the application and under Application URIs in Allowed callback URLs, Allowed logout URLs and Allowed Web origins add &lt;a href="https://localhost:3000" rel="noopener noreferrer"&gt;https://localhost:3000&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5uaofs6v1m7yu2sy2pj6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5uaofs6v1m7yu2sy2pj6.png" alt="Add Url" width="411" height="483"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We do this because Auth0 redirects us to their website which handles all of the authentications and redirects us back to the application.&lt;/p&gt;

&lt;p&gt;During development, it will be &lt;a href="https://localhost:3000" rel="noopener noreferrer"&gt;https://localhost:3000&lt;/a&gt; and in production, you should change it to your application URL.&lt;/p&gt;

&lt;h2&gt;
  
  
  React app
&lt;/h2&gt;

&lt;p&gt;Create a &lt;strong&gt;.env&lt;/strong&gt; in the root of the directory and add two variables&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;REACT_APP_AUTH0_DOMAIN = {domain}

REACT_APP_AUTH0_CLIENT_ID = {Client_ID} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Copy the Domain and Client ID from the settings of the application in Auth0.&lt;/p&gt;

&lt;h3&gt;
  
  
  Install Auth0
&lt;/h3&gt;

&lt;p&gt;Next, we have to install the Auth0 package in our application&lt;/p&gt;

&lt;p&gt;To do that run the following command in the terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i @auth0/auth0-react

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Index.js
&lt;/h2&gt;

&lt;p&gt;Open Index.js file in React application.&lt;/p&gt;

&lt;p&gt;The first thing we need is to import Auth0Provider from the Auth0 package we installed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Auth0Provider } from "@auth0/auth0-react";

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Auth0Provider uses React's Context API so we will wrap our App component with Auth0Provider to &lt;br&gt;
access all of its properties within our app &lt;/p&gt;

&lt;p&gt;Auth0Provider takes a couple of parameters i.e the domain and client id so we will import the environmental variables we created before.&lt;/p&gt;

&lt;p&gt;After all the changes your Index.js file should look something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// index.js

import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import { Auth0Provider } from "@auth0/auth0-react";
const domain = process.env.REACT_APP_AUTH0_DOMAIN;
const clientId = process.env.REACT_APP_AUTH0_CLIENT_ID;

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  &amp;lt;Auth0Provider
    domain={domain}
    clientId={clientId}
    redirectUri={window.location.origin}
  &amp;gt;
    &amp;lt;App /&amp;gt;
  &amp;lt;/Auth0Provider&amp;gt;
);


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see Auth0Provider takes in another parameter i.e the redirectUri that takes our current location so after logging in the user will be redirected to the current Url in the browser meaning our App.&lt;/p&gt;

&lt;h2&gt;
  
  
  Components
&lt;/h2&gt;

&lt;p&gt;Next, create a components folder under the src directory in our react app. In this app, we will create two components one for the login button and the other for the logout button. &lt;/p&gt;

&lt;h3&gt;
  
  
  Login Button
&lt;/h3&gt;

&lt;p&gt;Create a LoginButton.js component under the components directory.&lt;/p&gt;

&lt;p&gt;Import useAuth0 hook that is provided by the Auth0 package we installed&lt;/p&gt;

&lt;p&gt;We will use loginWithRedirect from the useAuth0 hook and we will return a login button with loginWithRedirect as the onClick event&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import { useAuth0 } from "@auth0/auth0-react";

export default function LoginButton() {
  const { loginWithRedirect } = useAuth0();
  return (
  &amp;lt;button onClick={() =&amp;gt; loginWithRedirect()}&amp;gt;
  Log In
  &amp;lt;/button&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Import the LoginButton component in App.js&lt;/p&gt;

&lt;p&gt;When you click this button it should redirect you to the Auth0 authentication form.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flgi97qxr0edantmrcyhu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flgi97qxr0edantmrcyhu.png" alt="Login form" width="407" height="563"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Logout Button
&lt;/h3&gt;

&lt;p&gt;Create another component but this time for the logout button&lt;/p&gt;

&lt;p&gt;This is similar to the login button the only difference is we will use logout method from the useAuth0 hook&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import { useAuth0 } from "@auth0/auth0-react";

export default function LogoutButton() {
  const { logout } = useAuth0();
  return (
    &amp;lt;button
      onClick={() =&amp;gt; {
        logout();
      }}
    &amp;gt;
      Log Out
    &amp;lt;/button&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  App.js
&lt;/h3&gt;

&lt;p&gt;Import and display both Login and Logout buttons&lt;/p&gt;

&lt;p&gt;Your App.js file should look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "./App.css";
import LoginButton from "./components/LoginButton";
import LogoutButton from "./components/LogoutButton";


function App() {
  return (
    &amp;lt;&amp;gt;
      &amp;lt;LoginButton /&amp;gt;
      &amp;lt;LogoutButton /&amp;gt;
    &amp;lt;/&amp;gt;
  );
}

export default App;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo2g9rhhde250f1y21d8w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo2g9rhhde250f1y21d8w.png" alt="Buttons" width="327" height="101"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  User
&lt;/h3&gt;

&lt;p&gt;Now to let the user know that they are logged in will just display the user information.&lt;/p&gt;

&lt;p&gt;To do that we will create another component User.js and display JSON user data&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import { useAuth0 } from "@auth0/auth0-react";

export default function User() {
  const { user } = useAuth0();
  return &amp;lt;div&amp;gt;{JSON.stringify(user, null, 2)}&amp;lt;/div&amp;gt;;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the user is logged in it will display the JSON data.&lt;/p&gt;

&lt;p&gt;Click on the login button and after logging in you should see the JSON data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Febg9bn9y2dft8gfgbwgx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Febg9bn9y2dft8gfgbwgx.png" alt="User Info" width="800" height="133"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's it for this blog. Drop a like if you found this useful :)&lt;/p&gt;

&lt;p&gt;Feel free to connect with me &lt;a href="https://bio.link/bhagatpratik07" rel="noopener noreferrer"&gt;https://bio.link/bhagatpratik07&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>auth</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Fetching Data in React</title>
      <dc:creator>Pratik Bhagat</dc:creator>
      <pubDate>Tue, 23 Nov 2021 15:24:46 +0000</pubDate>
      <link>https://dev.to/bhagatpratik07/fetching-data-in-react-hni</link>
      <guid>https://dev.to/bhagatpratik07/fetching-data-in-react-hni</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This blog post will teach you how to fetch data from an external API and use it in your React apps.&lt;/p&gt;

&lt;p&gt;Before you start reading you should be familiar with React,  &lt;a href="https://dev.to/bhagatpratik07/introduction-to-react-hooks-3h02"&gt;useState and useEffect hooks&lt;/a&gt;  in React.&lt;/p&gt;

&lt;h2&gt;
  
  
  Methods of Fetching Data
&lt;/h2&gt;

&lt;p&gt;We will look at the following ways to fetch data: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using Fetch API&lt;/li&gt;
&lt;li&gt;Using async function&lt;/li&gt;
&lt;li&gt;Using Axios&lt;/li&gt;
&lt;li&gt;Using custom hooks&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Using Fetch API
&lt;/h2&gt;

&lt;p&gt;The Fetch API provides a JavaScript interface for accessing and manipulating parts of the HTTP pipeline, such as requests and responses. It provides a global fetch( ) method that provides an easy, logical way to fetch resources asynchronously across the network.&lt;/p&gt;

&lt;p&gt;We will use the fetch( ) method which takes a single argument i.e the path you want to fetch data from and it returns a promise containing a response.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetch('https://jsonplaceholder.typicode.com/todos/1')
  .then(response =&amp;gt; response.json())
  .then(json =&amp;gt; console.log(json))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using async function
&lt;/h2&gt;

&lt;p&gt;The async function returns a promise and the await keyword makes the function wait for a response.&lt;/p&gt;

&lt;p&gt;Here's how we can use async/await to fetch data&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function fetchData() {
      const response = await fetch(
        "https://jsonplaceholder.typicode.com/todos/3"
      );
      const data = await response.json();
      console.log(data);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using Axios
&lt;/h2&gt;

&lt;p&gt;Axios is a library that is used to fetch data and it already gives the result in JSON, so we don't have to convert it.&lt;/p&gt;

&lt;p&gt;First, we have to install Axios with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install axios
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To use axios in our project we have to import it into our project&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import axios from "axios"

  React.useEffect(() =&amp;gt; {
    axios.get("https://jsonplaceholder.typicode.com/todos/3")
    .then((response) =&amp;gt; (console.log(response));
  }, []);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we used the .get() method to make a get request to our endpoint.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using custom hook
&lt;/h2&gt;

&lt;p&gt;We will make our custom react hook to fetch data which will take a single argument that is the endpoint we want to fetch the data from.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect } from "react";

const useFetch = (url) =&amp;gt; {
  useEffect(() =&amp;gt; {
    fetch(url)
      .then((response) =&amp;gt; response.json())
      .then((data) =&amp;gt; {
        console.log(data);
      });
  }, []);
};

export default useFetch;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Like this blog if you found it helpful and connect with me  on &lt;a href="https://twitter.com/bhagatpratik07" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; and  &lt;a href="https://www.linkedin.com/in/pratik-bhagat07/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;  &lt;/p&gt;

&lt;h2&gt;
  
  
  Thank you for reading ;)
&lt;/h2&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Next.js Authentication with Netlify Identity</title>
      <dc:creator>Pratik Bhagat</dc:creator>
      <pubDate>Tue, 27 Jul 2021 08:50:54 +0000</pubDate>
      <link>https://dev.to/bhagatpratik07/next-js-authentication-with-netlify-identity-3ghi</link>
      <guid>https://dev.to/bhagatpratik07/next-js-authentication-with-netlify-identity-3ghi</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Next.js is an open-source development framework built on top of Node.js enabling React-based web applications functionalities such as server-side rendering and generating static websites.&lt;/p&gt;

&lt;p&gt;In this guide, I will show you how to add authentication to your Next.js App using Netlify Identity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Basics of Next.js&lt;/p&gt;

&lt;p&gt;Context in React&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Next.js App
&lt;/h2&gt;

&lt;p&gt;First, create/select an app you would like to add authentication to. Deploy that app to Netlify so you can use Netlify Identity it's a service provided by Netlify to add authentication to your apps. &lt;/p&gt;

&lt;p&gt;If you don't have an app you can download the starter code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-next-app [your_project_name] -e https://github.com/bhagatpratik07/next-js-auth-blog/tree/blog

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: Change the name of the app before you run the command.&lt;/p&gt;

&lt;p&gt;Run the above command in the terminal. You will get a simple next.js application with Home and About Page. We will add authentication to this app. &lt;/p&gt;

&lt;h3&gt;
  
  
  Installing Netlify Identity in our App
&lt;/h3&gt;

&lt;p&gt;Netlify Identity is a service provided by Netlify to add authentication to our apps.&lt;/p&gt;

&lt;p&gt;Before we code we need to install Netlify identity in our app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install netlify-identity-widget
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the above command in the terminal to install Netlify Identity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deploying to Netlify
&lt;/h2&gt;

&lt;p&gt;Now that you have downloaded the starter code it's time to deploy it to Netlify. Head over to &lt;a href="https://www.netlify.com/" rel="noopener noreferrer"&gt;https://www.netlify.com/&lt;/a&gt; and deploy your site.  After deploying you should see something like this : &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwl88uj3oanhxc5jthctv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwl88uj3oanhxc5jthctv.png" alt="Screenshot (2).png" width="800" height="454"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, click on Identity in navbar and click on Enable Identity. Now Netlify Identity is enabled in our app.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's Start
&lt;/h2&gt;

&lt;p&gt;Create a new contexts directory at the top level of our application and inside contexts create a new file called authContext.js. Our file structure should look something like this : &lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6vwnvv5x6rvpfrpyf1mc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6vwnvv5x6rvpfrpyf1mc.png" alt="Screenshot (4).png" width="800" height="452"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Coding our authContext
&lt;/h2&gt;

&lt;p&gt;Add the below code to our AuthContext&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createContext} from "react";

export const AuthContext = createContext({
  user: null,
  login: () =&amp;gt; {},
  logout: () =&amp;gt; {},
  authReady: false,
});

const AuthContextProvider = ({ children }) =&amp;gt; {
  return &amp;lt;AuthContext.Provider&amp;gt;{children}&amp;lt;/AuthContext.Provider&amp;gt;;
};

export default AuthContextProvider;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First, we create our AuthContext and give it default values like user initially is set to null. We have default functions for login and logout. We have authReady state, which is a boolean it will keep track of when we have established a connection with Netlify Identity. The reason we have authReady state is that when our app runs in the browser it doesn't know if the user is logged in or not. &lt;/p&gt;

&lt;p&gt;Until we establish connection with Netlify authReady will be false, when we do establish a connection with Netlify it will be set to true.&lt;/p&gt;

&lt;p&gt;Also, note that we have to wrap our app (pages/_app.js) with our context as we need to access context in our entire app. NavBar and Component are now children to our Context.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// pages/_app.js

import NavBar from "../components/NavBar";
import AuthContextProvider from "../contexts/authContext";
import "../styles/globals.css";

function MyApp({ Component, pageProps }) {
  return (
    &amp;lt;AuthContextProvider&amp;gt;
      &amp;lt;NavBar /&amp;gt;
      &amp;lt;Component {...pageProps} /&amp;gt;
    &amp;lt;/AuthContextProvider&amp;gt;
  );
}

export default MyApp;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Update your pages/_app.js file accordingly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Establishing connection with Netlify Identity
&lt;/h2&gt;

&lt;p&gt;To establish a connection with netlify identity we will use the useEffect hook in react&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// contexts/authContext

import { createContext, useState, useEffect } from "react";
import { netlifyIdentity } from "netlify-identity-widget";

export const AuthContext = createContext({
  user: null,
  login: () =&amp;gt; {},
  logout: () =&amp;gt; {},
  authReady: false,
});

const AuthContextProvider = ({ children }) =&amp;gt; {
  useEffect(() =&amp;gt; {
    netlifyIdentity.init();
  }, []);
  return &amp;lt;AuthContext.Provider&amp;gt;{children}&amp;lt;/AuthContext.Provider&amp;gt;;
};

export default AuthContextProvider;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We import netlifyIdentity and inside our useEffect hook we simply need to add netlifyIdentity.init( ). All this will do is initialize connection with netlify identity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Providing value to our App
&lt;/h2&gt;

&lt;p&gt;Next, we need to provide login, logout, user values to our app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createContext, useState, useEffect } from "react";
import netlifyIdentity from "netlify-identity-widget";

export const AuthContext = createContext({
  user: null,
  login: () =&amp;gt; {},
  logout: () =&amp;gt; {},
  authReady: false,
});

const AuthContextProvider = ({ children }) =&amp;gt; {
  const [user, setUser] = useState(null);


  useEffect(() =&amp;gt; {
      // on login
    netlifyIdentity.on("login", (user) =&amp;gt; {
      setUser(user);
      netlifyIdentity.close();
    });

    // on logout
    netlifyIdentity.on("logout", (user) =&amp;gt; {
      setUser(null);
    });


    // connect with Netlify Identity
    netlifyIdentity.init();
  }, []);

  const login = () =&amp;gt; {
    netlifyIdentity.open();
  };


  const logout = () =&amp;gt; {
    netlifyIdentity.logout();
  };


  const context = {
    login,
    logout,
    user,
  };


  return (
    &amp;lt;AuthContext.Provider value={context}&amp;gt;
    {children}
    &amp;lt;/AuthContext.Provider&amp;gt;
  );
};

export default AuthContextProvider;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's a lot of code...let me explain&lt;/p&gt;

&lt;p&gt;1.Login&lt;/p&gt;

&lt;p&gt;We create a login function in which we just use open( ) which is a built-in function of Netlify Identity. All this does is open a login/signup form. We will use this as a callback function on the login button.&lt;/p&gt;

&lt;p&gt;2.Logout&lt;/p&gt;

&lt;p&gt;Logout function is the same as the login function but we use logout( ) which is a built-in netlify function to log out the user.&lt;/p&gt;

&lt;p&gt;Also, we update our useEffect and we add event listeners to log in and log out actions. &lt;/p&gt;

&lt;p&gt;On login, the user will be set to the user from null and we close our form.&lt;/p&gt;

&lt;p&gt;On logout, we set our user to null.&lt;/p&gt;

&lt;p&gt;And we provide all this as context to our app.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating Login/Logout Buttons
&lt;/h2&gt;

&lt;p&gt;Now it's time to update our navbar. Open components/navbar file in the code editor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// components/navbar

import styles from "../styles/Home.module.css";
import Link from "next/link";
import { useContext } from "react";
import { AuthContext } from "../contexts/authContext";

const NavBar = () =&amp;gt; {
  const { user, login, logout } = useContext(AuthContext);
  return (
    &amp;lt;nav className={styles.nav}&amp;gt;
      &amp;lt;Link href="/"&amp;gt;
        &amp;lt;a&amp;gt;Home&amp;lt;/a&amp;gt;
      &amp;lt;/Link&amp;gt;
      &amp;lt;Link href="/about"&amp;gt;
        &amp;lt;a&amp;gt;About&amp;lt;/a&amp;gt;
      &amp;lt;/Link&amp;gt;
      &amp;lt;li&amp;gt;{!user &amp;amp;&amp;amp; &amp;lt;button onClick={login}&amp;gt;Login/Signup&amp;lt;/button&amp;gt;}&amp;lt;/li&amp;gt;
      &amp;lt;li&amp;gt;{user &amp;amp;&amp;amp; &amp;lt;button onClick={logout}&amp;gt;Logout&amp;lt;/button&amp;gt;}&amp;lt;/li&amp;gt;
    &amp;lt;/nav&amp;gt;
  );
};

export default NavBar;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First, we access login, logout, user values from our context using the useContext hook.&lt;/p&gt;

&lt;p&gt;Then we create a login button which on click fires off the login function we created in our context. Same with the logout button. &lt;/p&gt;

&lt;p&gt;We conditionally render the buttons based on if the user is logged in or not.&lt;/p&gt;

&lt;p&gt;Now click on the login button and voila.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftzicyjs8l41dlmoh4nmt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftzicyjs8l41dlmoh4nmt.png" alt="Screenshot (5).png" width="800" height="301"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It will ask you for the website URL. You just have to enter the URL of the website you deployed on Netlify.&lt;/p&gt;

&lt;p&gt;You can also add external providers such as Google, Github as login options. For that head back to Netlify and go to Site Settings -&amp;gt; Identity (on sidebar) and on external providers select the options you want to include.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flci90sayc1hcg070p2os.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flci90sayc1hcg070p2os.png" alt="Screenshot (7).png" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;First, we created authContext to globally provide user values to our Apps. We added user and log-in, logout functions (built-in Netlify Identity functions) in our Context and used them as callback functions to our Login and Logout buttons.&lt;/p&gt;

&lt;h2&gt;
  
  
  Thank you for reading :)
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Drop a like and follow me if you found this useful 😊
&lt;/h2&gt;

</description>
      <category>react</category>
      <category>nextjs</category>
      <category>beginners</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Introduction to React Hooks</title>
      <dc:creator>Pratik Bhagat</dc:creator>
      <pubDate>Thu, 08 Jul 2021 14:53:26 +0000</pubDate>
      <link>https://dev.to/bhagatpratik07/introduction-to-react-hooks-3h02</link>
      <guid>https://dev.to/bhagatpratik07/introduction-to-react-hooks-3h02</guid>
      <description>&lt;h3&gt;
  
  
  What are Hooks?
&lt;/h3&gt;

&lt;p&gt;Hooks allow us to use state and lifecycle features in function components that were specific to class components before React 16.8 &lt;/p&gt;

&lt;h3&gt;
  
  
  Benefits of React Hooks
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Less Code&lt;/li&gt;
&lt;li&gt;Easily Readable&lt;/li&gt;
&lt;li&gt;Easier to decouple logic from UI&lt;/li&gt;
&lt;li&gt;Performance boost with functional components&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  useState Hook
&lt;/h3&gt;

&lt;p&gt;The first hook we will look at is the useState hook. The useState hook allows us to add state to a functional component.&lt;/p&gt;

&lt;p&gt;Syntax: The first element is the initial state and the second one is a function that is used for updating the state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [count, setCount] = React.useState(0)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, we declare a  variable count which holds the value 0 and setCount is the function to update the count. Executing this function re-renders the component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's make a counter using the useState hook&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from "react";

export default function Counter() {
  const [count, setCount] = useState(0);

  const incrementCount = () =&amp;gt; {
    setCount(count + 1);
  };
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;div&amp;gt;{count}&amp;lt;/div&amp;gt;
      &amp;lt;button onClick={incrementCount}&amp;gt;increment&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First, we initialize our count to 0, and then we created a function incrementCount which uses setCount to increase our count by 1. We display our count and a button which on click calls the incrementCount function.&lt;/p&gt;

&lt;h3&gt;
  
  
  useEffect Hook
&lt;/h3&gt;

&lt;p&gt;In class components,  you may be familiar with lifecycle methods. The lifecycle methods, componentDidMount, componentDidUpdate, and componentWillUnmount, are all handled by the useEffect hook in functional components. The Effect Hook lets you perform side effects in function components. Examples of side effects are fetching data, changing the DOM. Fetching data is one of the most common uses of the useEffect hook. The useEffect hook needs at least one argument or a function to execute. You can also add a cleanup function inside the useEffect hook.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(()=&amp;gt;{
    // will be called on initial render and every single re-render
})

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The useEffect hook also takes an array after the function which is called the dependency array. Where we can pass dependencies on which our effect depends on. Let's take a look at an example :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(() =&amp;gt; {
    console.log("count")
}, [count])

useEffect(()=&amp;gt;{
    console.log("render")
},[])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, count is our dependency so console.log("count") will only execute when the count changes. If we leave our dependency array empty console.log(" &lt;br&gt;
render") will only execute on the initial render.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect( ()  =&amp;gt;  {
    effect
    return () =&amp;gt; {
        cleanup
    }
}, [value])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside useEffect, we can add a return statement at the end of the function call which returns a function. This return function does all the cleanup work. It's similar to componentWillUnmount in class component. Note that adding a return statement is optional in useEffect.&lt;/p&gt;

&lt;h3&gt;
  
  
  Thank you for reading.
&lt;/h3&gt;

&lt;h3&gt;
  
  
  If you found this useful drop a like :)
&lt;/h3&gt;

&lt;p&gt;Read more about hooks 👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://reactjs.org/docs/hooks-intro.html" rel="noopener noreferrer"&gt;React Docs&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://blog.bitsrc.io/6-reasons-to-use-react-hooks-instead-of-classes-7e3ee745fe04" rel="noopener noreferrer"&gt;Classes vs Hooks&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/@vcarl/everything-you-need-to-know-about-react-hooks-8f680dfd4349" rel="noopener noreferrer"&gt;Hooks explained&lt;/a&gt; &lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
