<?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>Forem: Utsav Mavani</title>
    <description>The latest articles on Forem by Utsav Mavani (@utsavmavani).</description>
    <link>https://forem.com/utsavmavani</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%2F1224128%2F6bd18134-ab6e-480a-ae66-8ba06d66299b.png</url>
      <title>Forem: Utsav Mavani</title>
      <link>https://forem.com/utsavmavani</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/utsavmavani"/>
    <language>en</language>
    <item>
      <title>React.js Page Transition With Framer-Motion layout .</title>
      <dc:creator>Utsav Mavani</dc:creator>
      <pubDate>Fri, 02 Feb 2024 04:31:13 +0000</pubDate>
      <link>https://forem.com/utsavmavani/reactjs-page-transition-with-framer-motion-layout--10l0</link>
      <guid>https://forem.com/utsavmavani/reactjs-page-transition-with-framer-motion-layout--10l0</guid>
      <description>&lt;p&gt;Certainly! To implement page transitions in a React.js application using Framer Motion, you can follow these steps. I'll provide a simple example using Framer Motion's &lt;strong&gt;motion&lt;/strong&gt; and &lt;strong&gt;AnimatePresence&lt;/strong&gt; components for page transitions.&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 framer-motion react-router-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a layout component that will handle the page transitions. Let's call it &lt;strong&gt;Layout.js&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;// Layout.js
import React from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import { useLocation } from 'react-router-dom';

const Layout = ({ children }) =&amp;gt; {
  const location = useLocation();

  return (
    &amp;lt;AnimatePresence exitBeforeEnter&amp;gt;
      &amp;lt;motion.div
        key={location.pathname}
        initial={{ opacity: 0 }}
        animate={{ opacity: 1 }}
        exit={{ opacity: 0 }}
      &amp;gt;
        {children}
      &amp;lt;/motion.div&amp;gt;
    &amp;lt;/AnimatePresence&amp;gt;
  );
};

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

&lt;/div&gt;



&lt;p&gt;In this component, we are using &lt;strong&gt;AnimatePresence&lt;/strong&gt; to handle page transitions. The &lt;strong&gt;key&lt;/strong&gt; attribute ensures that Framer Motion animates when the page changes.&lt;/p&gt;

&lt;p&gt;Set up your main &lt;strong&gt;App.js&lt;/strong&gt; file using React Router and the &lt;strong&gt;Layout&lt;/strong&gt; component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// App.js
import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import Layout from './Layout';
import Home from './Home'; // Create your Home component
import About from './About'; // Create your About component

const App = () =&amp;gt; {
  return (
    &amp;lt;Router&amp;gt;
      &amp;lt;Layout&amp;gt;
        &amp;lt;Switch&amp;gt;
          &amp;lt;Route path="/about" component={About} /&amp;gt;
          &amp;lt;Route path="/" component={Home} /&amp;gt;
        &amp;lt;/Switch&amp;gt;
      &amp;lt;/Layout&amp;gt;
    &amp;lt;/Router&amp;gt;
  );
};

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

&lt;/div&gt;



&lt;p&gt;Replace &lt;strong&gt;Home&lt;/strong&gt; and &lt;strong&gt;About&lt;/strong&gt; with your actual page components.&lt;/p&gt;

&lt;p&gt;Now, you can create your page components, such as &lt;strong&gt;Home.js&lt;/strong&gt; and &lt;strong&gt;About.js&lt;/strong&gt;. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Home.js
import React from 'react';

const Home = () =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Home Page&amp;lt;/h1&amp;gt;
      &amp;lt;p&amp;gt;Welcome to the home page!&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

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

&lt;/div&gt;



&lt;p&gt;Now, when you navigate between the home and about pages, Framer Motion will handle the page transitions with a fade effect. Customize the animations and transitions based on your specific needs&lt;/p&gt;




&lt;p&gt;Fade and scale transition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Layout.js
import React from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import { useLocation } from 'react-router-dom';

const Layout = ({ children }) =&amp;gt; {
  const location = useLocation();

  return (
    &amp;lt;AnimatePresence exitBeforeEnter&amp;gt;
      &amp;lt;motion.div
        key={location.pathname}
        initial={{ opacity: 0, scale: 0.8 }}
        animate={{ opacity: 1, scale: 1 }}
        exit={{ opacity: 0, scale: 0.8 }}
        transition={{ duration: 0.5 }}
      &amp;gt;
        {children}
      &amp;lt;/motion.div&amp;gt;
    &amp;lt;/AnimatePresence&amp;gt;
  );
};

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

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>animation</category>
    </item>
    <item>
      <title>Javascript scroll snippets</title>
      <dc:creator>Utsav Mavani</dc:creator>
      <pubDate>Fri, 19 Jan 2024 16:14:26 +0000</pubDate>
      <link>https://forem.com/utsavmavani/javascript-scroll-3bgp</link>
      <guid>https://forem.com/utsavmavani/javascript-scroll-3bgp</guid>
      <description>&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Scroll to Top&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function scrollToTop() {
  window.scrollTo({ top: 0, behavior: 'smooth' });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;scrollToTop();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Scroll to Element Smoothly&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function scrollToElement(element) {
  element.scrollIntoView({ behavior: 'smooth' });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myElement = document.getElementById('myElement');
scrollToElement(myElement);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Load More button in a React.js</title>
      <dc:creator>Utsav Mavani</dc:creator>
      <pubDate>Fri, 19 Jan 2024 16:08:33 +0000</pubDate>
      <link>https://forem.com/utsavmavani/load-more-button-in-a-reactjs-5dgo</link>
      <guid>https://forem.com/utsavmavani/load-more-button-in-a-reactjs-5dgo</guid>
      <description>&lt;p&gt;To implement a "Load More" button in a React.js application, you typically fetch additional data from an API or another data source when the button is clicked. Here's a basic example using the &lt;strong&gt;useState&lt;/strong&gt; hook to manage the state of the displayed data and a counter to keep track of how many items to display.&lt;/p&gt;

&lt;p&gt;Assuming you have an array of data and want to display a certain number of items initially, then load more when the button is clicked, you can follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Set up your component with state and a handler for the "Load More" button:
&lt;/li&gt;
&lt;/ol&gt;

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

const YourComponent = () =&amp;gt; {
  // Your initial data
  const initialData = [
    // ... your data array
  ];

  // State to manage displayed data and load more button
  const [displayedData, setDisplayedData] = useState(initialData);
  const [visibleItemCount, setVisibleItemCount] = useState(5); // Change this as needed

  // Handler for the "Load More" button
  const handleLoadMore = () =&amp;gt; {
    // Increase the visible item count (you can adjust this logic based on your requirements)
    setVisibleItemCount(prevCount =&amp;gt; prevCount + 5);

    // Update displayedData with additional items
    setDisplayedData(initialData.slice(0, visibleItemCount + 5));
  };

  return (
    &amp;lt;div&amp;gt;
      {displayedData.map((item, index) =&amp;gt; (
        // Render your data items here
        &amp;lt;div key={index}&amp;gt;{item}&amp;lt;/div&amp;gt;
      ))}
      &amp;lt;button onClick={handleLoadMore}&amp;gt;Load More&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

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

&lt;/div&gt;



&lt;p&gt;Customize the logic inside the &lt;strong&gt;handleLoadMore&lt;/strong&gt; function based on your specific requirements. This example assumes a simple approach of increasing the visible item count and updating the displayed data accordingly.&lt;/p&gt;

&lt;p&gt;Keep in mind that this is a basic example, and you might need to adapt it based on the structure of your data and the way you fetch additional items (e.g., from an API). If you're fetching data asynchronously, you might use &lt;strong&gt;useEffect&lt;/strong&gt; and an asynchronous function within &lt;strong&gt;handleLoadMore&lt;/strong&gt; to fetch and update the data.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Tab logic using ReactJS</title>
      <dc:creator>Utsav Mavani</dc:creator>
      <pubDate>Fri, 19 Jan 2024 16:00:58 +0000</pubDate>
      <link>https://forem.com/utsavmavani/tab-logic-using-reactjs-2j6o</link>
      <guid>https://forem.com/utsavmavani/tab-logic-using-reactjs-2j6o</guid>
      <description>&lt;p&gt;how you can implement a simple tab logic using ReactJS. In this example, I'll use functional components and hooks. Let's create a component called Tabs:&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';
import './Tabs.css'; // Import your CSS file for styling

const Tabs = () =&amp;gt; {
  const tabs = ['Tab 1', 'Tab 2', 'Tab 3', 'Tab 4'];
  const [activeTab, setActiveTab] = useState(0);

  const handleTabClick = (index) =&amp;gt; {
    setActiveTab(index);
  };

  return (
    &amp;lt;div className="tabs-container"&amp;gt;
      &amp;lt;div className="left-side"&amp;gt;
        &amp;lt;h2&amp;gt;{tabs[activeTab]}&amp;lt;/h2&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div className="right-side"&amp;gt;
        {tabs.map((tab, index) =&amp;gt; (
          &amp;lt;div
            key={index}
            className={`tab ${activeTab === index ? 'active' : ''}`}
            onClick={() =&amp;gt; handleTabClick(index)}
          &amp;gt;
            {tab}
          &amp;lt;/div&amp;gt;
        ))}
      &amp;lt;/div&amp;gt;
      &amp;lt;div className="content"&amp;gt;
        {/* Render content based on active tab */}
        &amp;lt;div className={`tab-content ${activeTab === 0 ? 'active' : ''}`}&amp;gt;
          Content for Tab 1
        &amp;lt;/div&amp;gt;
        &amp;lt;div className={`tab-content ${activeTab === 1 ? 'active' : ''}`}&amp;gt;
          Content for Tab 2
        &amp;lt;/div&amp;gt;
        &amp;lt;div className={`tab-content ${activeTab === 2 ? 'active' : ''}`}&amp;gt;
          Content for Tab 3
        &amp;lt;/div&amp;gt;
        &amp;lt;div className={`tab-content ${activeTab === 3 ? 'active' : ''}`}&amp;gt;
          Content for Tab 4
        &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default Tabs;

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

&lt;/div&gt;



&lt;p&gt;In this example, we have a Tabs component with an array of tab titles (tabs) and a state variable (activeTab) to keep track of the currently active tab. The handleTabClick function is used to update the active tab when a tab is clicked.&lt;/p&gt;

&lt;p&gt;Make sure to create a CSS file (e.g., Tabs.css) to style your tabs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.tabs-container {
  display: flex;
  width: 100%;
}

.left-side {
  flex: 1;
}

.right-side {
  display: flex;
}

.tab {
  padding: 10px;
  cursor: pointer;
  border: 1px solid #ccc;
  margin-right: 5px;
}

.tab.active {
  background-color: #ddd;
}

.content {
  width: 100%;
  margin-top: 10px;
}

.tab-content {
  display: none;
}

.tab-content.active {
  display: block;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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