<?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: Toufic Nabi</title>
    <description>The latest articles on DEV Community by Toufic Nabi (@touficnabi).</description>
    <link>https://dev.to/touficnabi</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%2F1073425%2F60fb6f0e-5e46-4b95-9585-98be7d923b39.png</url>
      <title>DEV Community: Toufic Nabi</title>
      <link>https://dev.to/touficnabi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/touficnabi"/>
    <language>en</language>
    <item>
      <title>React-hook-form validation</title>
      <dc:creator>Toufic Nabi</dc:creator>
      <pubDate>Sat, 10 Jun 2023 23:44:52 +0000</pubDate>
      <link>https://dev.to/touficnabi/react-hook-form-validation-4go6</link>
      <guid>https://dev.to/touficnabi/react-hook-form-validation-4go6</guid>
      <description>&lt;p&gt;When working with form in react projects, react-hook-form can be a lifesaver. This provides a great opportunity for us to handle form easily and handle the form validation efficiently.&lt;br&gt;
Here's how you can add it to your project.&lt;/p&gt;

&lt;p&gt;Necessary packages:&lt;br&gt;
&lt;a href="https://www.npmjs.com/package/react-hook-form"&gt;react-hook-form&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.npmjs.com/package/yup"&gt;yup&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.npmjs.com/package/@hookform/resolvers"&gt;@hookform/resolvers&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt;&lt;br&gt;
Install all those 3 packages&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 react-hook-form yup @hookform/resolvers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 2: &lt;br&gt;
Design your form like follows&lt;br&gt;
 WIP&lt;/p&gt;

</description>
      <category>reacthookform</category>
      <category>react</category>
      <category>form</category>
      <category>validation</category>
    </item>
    <item>
      <title>How to validate react form in the front-end.</title>
      <dc:creator>Toufic Nabi</dc:creator>
      <pubDate>Wed, 07 Jun 2023 10:39:59 +0000</pubDate>
      <link>https://dev.to/touficnabi/how-validate-react-form-in-the-front-end-5dm0</link>
      <guid>https://dev.to/touficnabi/how-validate-react-form-in-the-front-end-5dm0</guid>
      <description>&lt;p&gt;Validating form in react/next app in the front end can be a tedious process. But following these simple steps you can validate your form in react app in the front end very easily. &lt;/p&gt;

&lt;p&gt;Here's the code how you can implement that.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/brv9om"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>form</category>
      <category>validation</category>
      <category>react</category>
      <category>frontend</category>
    </item>
    <item>
      <title>How to add dark theme easily in NextJS using Zustand and Tailwind.</title>
      <dc:creator>Toufic Nabi</dc:creator>
      <pubDate>Sun, 28 May 2023 11:30:46 +0000</pubDate>
      <link>https://dev.to/touficnabi/how-to-add-dark-theme-easily-in-nextjs-using-zustand-and-tailwind-1f3g</link>
      <guid>https://dev.to/touficnabi/how-to-add-dark-theme-easily-in-nextjs-using-zustand-and-tailwind-1f3g</guid>
      <description>&lt;p&gt;Ability to switching theme in your website gives your user a great user-experience. All modern websites nowadays have this feature where you can switch theme between dark and light with a click of a button. Also, the theme preference needs to be store so when the user comes back to the website they have their selected option right away. &lt;/p&gt;

&lt;p&gt;Today we will implement theme switching to a Nextjs website using State Management Tool called &lt;a href="https://www.npmjs.com/package/zustand"&gt;Zustand&lt;/a&gt; and &lt;a href="https://tailwindcss.com/"&gt;Tailwind&lt;/a&gt;. (We could do it using context API but Zustand will be useful in other part of this project, so I used Zustand).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; &lt;br&gt;
Assuming you have installed and added tailwind in your project. If not, &lt;a href="https://tailwindcss.com/docs/guides/nextjs"&gt;follow these instructions&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Install Zustand: Go into the terminal in vscode in your project folder, run 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 i zustand
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then create a folder in src folder if your project called &lt;code&gt;store&lt;/code&gt;. Create a file called &lt;code&gt;theme.store.js&lt;/code&gt; inside of the store folder. (These are the naming and folder structure convention)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; &lt;br&gt;
Access the html tag&lt;br&gt;
We have to use the html tag for switching theme using a data attribute called &lt;code&gt;data-mode&lt;/code&gt;. To access the html tag in the nextjs project we have to use &lt;a href="https://nextjs.org/docs/pages/building-your-application/routing/custom-document"&gt;_document.js&lt;/a&gt; component provide by the nextjs team. This component goes inside of the pages folder for page based route system or app folder for App based route system. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; &lt;br&gt;
Add the default theme in html attribute (data-mode) 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;&amp;lt;Html lang='en-us' data-mode="light"&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;Main /&amp;gt;
    &amp;lt;NextScript /&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/Html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Edit the tailwind config&lt;br&gt;
Let's edit how the dark theme works in Tailwind. Inside of &lt;code&gt;tailwind.config.js&lt;/code&gt; file, add the &lt;code&gt;darkMode&lt;/code&gt; key value pair like follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  darkMode: ['class', '[data-mode="dark"]'],
  // other stuffs
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Theme switch button&lt;br&gt;
Let's make a button that can switch our theme. Usually, the button goes into the header. Make a button you like. This can be a toggle button or a regular button. We will add the onClick event in step 7. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; Create the store&lt;br&gt;
When using Zustand, the state management file is called a store. Head over to the theme.store.js file and add the theme element like follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { create } from 'zustand';
import { persist } from 'zustand/middleware';

const useStore = create(persist(
    (set, get) =&amp;gt; ({
        theme: "dark",
        setTheme: () =&amp;gt; set((state) =&amp;gt; ({
            ...state,
            theme: get().theme === "dark" ? "light" : "dark"
        })),
    }), {
        name: 'theme', // name of the item in the storage (must be unique)
    }
));

export const useTheme = () =&amp;gt; useStore((state) =&amp;gt; state.theme);
export const useSetTheme = () =&amp;gt; useStore((state) =&amp;gt; state.setTheme);

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

&lt;/div&gt;



&lt;p&gt;This will create, switch and store the theme in the localStorage of the browser. &lt;/p&gt;

&lt;p&gt;Now we have access to the theme state and setTheme function in all the components throughout the project. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7:&lt;/strong&gt; &lt;br&gt;
Import the theme, and setTheme items from the store to the header where your theme switch button is located like follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {useTheme, useSetTheme} from '../store/theme.store';
const Header = () =&amp;gt; {
  ...
  //get the values
  const theme = useTheme();
  const setTheme = useSetTheme();

  return (
    //your stuffs.
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then set the buttons onClick to the &lt;code&gt;setTheme&lt;/code&gt; function. This action will switch the theme between dark and light, and store the value in localStorage.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 8:&lt;/strong&gt;&lt;br&gt;
Now head over to the _app.js file, inside of useEffect hook, grab the localStorage value for the theme 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;const theme = useTheme();

useEffect(() =&amp;gt; {
  try {
    const localTheme = JSON.parse(localStorage.getItem('theme'));
    if (localTheme) {
      document.documentElement.setAttribute('data-mode', localTheme.state.theme);
      document.documentElement.className = localTheme.state.theme;
    }
  } catch (err) {
    console.log('error loading the color theme')
  }
}, [theme])

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

&lt;/div&gt;



&lt;p&gt;Of course, you have to import the &lt;code&gt;theme&lt;/code&gt; from the store for this as well. &lt;br&gt;
We are using the &lt;code&gt;theme&lt;/code&gt; as dependency so when our theme switch button updates the theme our value in html tag also changes from the localStorage. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 9:&lt;/strong&gt; add classNames for dark theme&lt;br&gt;
Now, go to any of your components or pages and add the class name for dark theme like follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div className="text-black bg:white dark:text-white dark:bg-black"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see the theme switching and retaining the preference upon reloading the page. &lt;br&gt;
Done!&lt;/p&gt;

&lt;p&gt;Enjoy!&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>zustand</category>
      <category>tailwindcss</category>
      <category>theme</category>
    </item>
    <item>
      <title>Install Google reCaptcha in your React/Next js app.</title>
      <dc:creator>Toufic Nabi</dc:creator>
      <pubDate>Sun, 14 May 2023 08:07:35 +0000</pubDate>
      <link>https://dev.to/touficnabi/install-google-recaptcha-in-your-reactnext-js-app-3376</link>
      <guid>https://dev.to/touficnabi/install-google-recaptcha-in-your-reactnext-js-app-3376</guid>
      <description>&lt;p&gt;To prevent the bots from sending you thousand message a day from your contact form, you should use some system to detect if the visitor is a real human or a bot pretending to be a human.&lt;/p&gt;

&lt;p&gt;To do so, you can use google reCaptcha. reCaptcha can determine if a visitor is a human or a bot using a score. Thats all done at their end so we do not have to worry about that. This article will show how we can add google reCaptcha v3 or invisible captcha into a reactjs/nextjs website. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt;&lt;br&gt;
Install &lt;a href="https://www.npmjs.com/package/react-google-recaptcha-v3"&gt;&lt;strong&gt;react-google-recaptcha-v3&lt;/strong&gt;&lt;/a&gt; into your project by running this in your ternimal in vscode.&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 react-google-recaptcha-v3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt;&lt;br&gt;
Then go to the &lt;a href="https://www.google.com/recaptcha/admin/create"&gt;admin console&lt;/a&gt; of the google reCaptcha. You have to login using your gmail account.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt;&lt;br&gt;
Register a new site&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add a label (can be your site name or any name you prefer)&lt;/li&gt;
&lt;li&gt;Select recaotcha type to v3 (score based)&lt;/li&gt;
&lt;li&gt;Add your domain(s) &lt;/li&gt;
&lt;li&gt;Agree to the terms and conditions and &lt;/li&gt;
&lt;li&gt;Press Submit&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt;&lt;br&gt;
You will get 2 keys under &lt;strong&gt;reCaptcha Keys&lt;/strong&gt; section&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt;&lt;br&gt;
Copy the site key because thats the key we need for this project&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt;&lt;br&gt;
Go to your project in vscode, and go to file you want to add your google reCaptcha v3 (for this example we will add to the contact page where our form lives)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7:&lt;/strong&gt;&lt;br&gt;
Import 2 utilities from the package we have installed in step 1&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { GoogleReCaptcha, GoogleReCaptchaProvider } from "react-google-recaptcha-v3";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 8:&lt;/strong&gt;&lt;br&gt;
Wrap the entire component with the provider 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;const contact = () =&amp;gt; {
    return (
        &amp;lt;GoogleReCaptchaProvider reCaptchaKey="&amp;lt;YOUR SITE KEY FROM STEP 5&amp;gt;"&amp;gt;
            //rest of your code goes here
        &amp;lt;/GoogleReCaptchaProvider&amp;gt;
    )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 9:&lt;/strong&gt;&lt;br&gt;
We need a state to store the reCaptcha value, so lets import the setState function from react and initiate a 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 [captcha, setCaptcha] = useState(null);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step: 10&lt;/strong&gt;&lt;br&gt;
Inside of your form, before the submit button add the capthca component we imported in step 7&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;textarea {...register("Message", {})} placeholder="Your Message" className='message' /&amp;gt;
&amp;lt;GoogleReCaptcha onVerify={(val) =&amp;gt; setCaptcha(val)} /&amp;gt;
&amp;lt;input type="submit" value="Send" className='submit' /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will set the captcha value to the captcha state we made. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step: 11&lt;/strong&gt;&lt;br&gt;
Now inside your onSubmit function you can conditionally send the message or reject it based on the captcha value&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const onSubmit = data =&amp;gt; {
    if (captcha) {
        console.log(data);
        //send the mail
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Voilà! you are all set. Now bots can not annoy you with junk mails. &lt;/p&gt;

</description>
      <category>recaptcha</category>
      <category>nextjs</category>
      <category>react</category>
      <category>form</category>
    </item>
    <item>
      <title>Deploying Next.JS app in Godaddy shared hosting correctly.</title>
      <dc:creator>Toufic Nabi</dc:creator>
      <pubDate>Sat, 29 Apr 2023 12:29:38 +0000</pubDate>
      <link>https://dev.to/touficnabi/deploying-nextjs-app-in-godaddy-shared-hosting-correctly-3kga</link>
      <guid>https://dev.to/touficnabi/deploying-nextjs-app-in-godaddy-shared-hosting-correctly-3kga</guid>
      <description>&lt;p&gt;Hosting a react app (next.js) in Godaddy Shared hosting can be a confusing job. Specially if you are using server side rendering. Godaddy shared hosting (in most cases) does not allow node apps. &lt;/p&gt;

&lt;p&gt;But we can take advantage of the Static generation feature. Using Static generation we can generate a static html site. You can follow the following steps: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go to package.json &lt;/li&gt;
&lt;li&gt;&lt;p&gt;Under Scripts object add a new key value pair like this &lt;br&gt;
&lt;code&gt;"genetate": "next build &amp;amp;&amp;amp; next export"&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;then, navigate to next.config.js&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add the following code:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  output: 'export',
  distDir: 'dist',
  trailingSlash: true,
  assetPrefix: '.',
}

module.exports = nextConfig;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then from your terminal run the command:&lt;br&gt;
npm run generate&lt;/p&gt;

&lt;p&gt;Important thing to note here is the &lt;code&gt;trailingSlash: true,&lt;/code&gt; &lt;br&gt;
In godaddy if you navigate the site using the say navigation bar it works but if you reload a page other than the home page &lt;br&gt;
for example /about/&lt;br&gt;
it looks for the about directory which does not exist. So with trailingSlash: true, this command your pages will go on their respective directory. &lt;/p&gt;

</description>
      <category>godaddy</category>
      <category>nextjs</category>
      <category>hosting</category>
      <category>routing</category>
    </item>
    <item>
      <title>Smooth onboarding and really happy with the UX here at Dev.to</title>
      <dc:creator>Toufic Nabi</dc:creator>
      <pubDate>Fri, 28 Apr 2023 13:05:09 +0000</pubDate>
      <link>https://dev.to/touficnabi/smooth-onboarding-and-really-happy-with-the-ux-here-at-devto-3i86</link>
      <guid>https://dev.to/touficnabi/smooth-onboarding-and-really-happy-with-the-ux-here-at-devto-3i86</guid>
      <description>&lt;p&gt;Developers and programmers are continuously looking for platforms that not only offer useful knowledge but also a seamless onboarding process and great user experience (UX) in the fast-paced world of technology. A standout example is Dev.to, which offers a simple onboarding process and leaves users feeling truly pleased with their whole experience. This essay will examine the distinctive features of Dev.to's onboarding and user experience.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A Friendly Environment:&lt;/strong&gt; When you first arrive to Dev.to, you are met with a friendly interface that encourages exploration. The platform's clean design, logical layout, and intelligently arranged pieces make it simple to navigate. Whether you're a seasoned developer or a beginner, Dev.to's user-friendly layout allows you to quickly and easily find what you're searching for.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Customizable Interests and Feeds:&lt;/strong&gt; Dev.to understands that each developer has different interests and preferences. During the onboarding process, you can choose your areas of interest, programming languages, and topics to investigate. This personalized approach guarantees that your feed is tuned to your unique requirements, giving a curated experience that corresponds to your preferences. This personalization increases user engagement and fosters a sense of belonging to the Dev.to community.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Collaboration and Seamless publication:&lt;/strong&gt; One of Dev.to's major advantages is the simplicity of the publication procedure. The platform's user-friendly editor makes it simple to create and share your own content. The writing process is easy, allowing you to concentrate on your ideas rather than becoming sidetracked by complicated technical issues. Additionally, Dev.to promotes collaboration by allowing numerous authors to work on the same content, encouraging teamwork and the sharing of collective expertise.&lt;/p&gt;

&lt;p&gt;Dev.to is the perfect example of what a simple onboarding procedure and top-notch UX should look like. When you first sign up for the platform, you are met with a friendly atmosphere and a tailored experience that is tailored to your interests. An excellent user experience is made possible by Dev.to's compelling content, active community, frictionless publishing, and helpful support. Dev.to offers a platform where you can learn, contribute, and grow alongside a welcoming community, regardless of your level of coding experience. It makes sense that users are content with Dev.to's onboarding procedure and user experience as a whole.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>ux</category>
      <category>devto</category>
      <category>blog</category>
    </item>
  </channel>
</rss>
