<?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: Ion Bragaru</title>
    <description>The latest articles on Forem by Ion Bragaru (@bragaru-i).</description>
    <link>https://forem.com/bragaru-i</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%2F637995%2Fb9024421-4c75-4b43-b017-01b27ae7c038.jpeg</url>
      <title>Forem: Ion Bragaru</title>
      <link>https://forem.com/bragaru-i</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/bragaru-i"/>
    <language>en</language>
    <item>
      <title>React Performance: Boost it with Tree Shaking</title>
      <dc:creator>Ion Bragaru</dc:creator>
      <pubDate>Thu, 12 Dec 2024 07:42:47 +0000</pubDate>
      <link>https://forem.com/bragaru-i/react-performance-boost-it-with-tree-shaking-3alp</link>
      <guid>https://forem.com/bragaru-i/react-performance-boost-it-with-tree-shaking-3alp</guid>
      <description>&lt;p&gt;&lt;strong&gt;Tree shaking&lt;/strong&gt; is an essential technique in modern JavaScript development, especially for libraries like &lt;code&gt;React&lt;/code&gt;. It helps &lt;strong&gt;eliminate unused code&lt;/strong&gt; from the final bundle, resulting in smaller file sizes and faster application performance.&lt;/p&gt;

&lt;p&gt;or as &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Tree_shaking" rel="noopener noreferrer"&gt;MDN says&lt;/a&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tree shaking&lt;/strong&gt; is a term commonly used within a JavaScript context to describe the removal of dead code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What is Tree Shaking?
&lt;/h2&gt;

&lt;p&gt;Tree shaking is a term used in JavaScript bundlers (like &lt;code&gt;Webpack&lt;/code&gt; and &lt;code&gt;Rollup&lt;/code&gt;) to describe the process of &lt;strong&gt;removing unused code&lt;/strong&gt; from your final JavaScript bundle. It works by analyzing the &lt;code&gt;import/export&lt;/code&gt; statements in your codebase, figuring out which parts of your code are actually being used, and eliminating everything else.&lt;/p&gt;

&lt;p&gt;The name "tree shaking" comes from the analogy of shaking a tree and letting the unused branches (code) fall off, leaving only the necessary parts in your final build.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Should React Developers Care About Tree Shaking?
&lt;/h2&gt;

&lt;p&gt;As a React developer, reducing the size of your JavaScript bundles can have a significant impact on the performance of your application. Large bundles lead to slow load times and a poor user experience, especially on mobile networks. Tree shaking helps ensure that only the code you actually use is included in your final build, which can dramatically improve performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Does Tree Shaking Work in React?
&lt;/h2&gt;

&lt;p&gt;Tree shaking relies heavily on &lt;strong&gt;ES Modules (ESM)&lt;/strong&gt;. When you import and export modules using ESM syntax, bundlers like &lt;code&gt;Webpack&lt;/code&gt; and &lt;code&gt;Rollup&lt;/code&gt; can analyze your code and remove unused exports.&lt;/p&gt;

&lt;p&gt;For example, let’s say you're using a large utility library, but you only need one function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Bad practice: importing everything&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;lodash&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lodash&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In this case, even if you're only using one function, the entire lodash library will be bundled. This is inefficient and increases your app size.&lt;/p&gt;

&lt;p&gt;Instead, you can use &lt;strong&gt;named imports&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Good practice: importing only what you need&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;debounce&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lodash&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way, only the &lt;code&gt;debounce&lt;/code&gt; function is included in your bundle, and the rest of the &lt;code&gt;lodash&lt;/code&gt; code is excluded, making your app much smaller.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices for Enabling Tree Shaking in React:
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Use ES Modules (ESM):
&lt;/h3&gt;

&lt;p&gt;Tree shaking works best when your code and the libraries you use are written in ESM. Avoid CommonJS if possible, as it doesn't support tree shaking.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Import Only What You Need:
&lt;/h3&gt;

&lt;p&gt;As shown above, always use named imports or import specific functions instead of the entire library.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Eliminate Side Effects:
&lt;/h3&gt;

&lt;p&gt;Tree shaking relies on the assumption that importing a module doesn’t have side effects. A "side effect" could be anything that changes global state, modifies variables, or executes code outside of the module itself. Ensure your modules don’t have unintended side effects that could prevent tree shaking from working.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Configure Your Bundler:
&lt;/h3&gt;

&lt;p&gt;Ensure that your bundler (like Webpack or Rollup) is properly configured for tree shaking. This often includes enabling production mode to enable optimizations.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Leverage &lt;code&gt;package.json&lt;/code&gt; "sideEffects" Field:
&lt;/h3&gt;

&lt;p&gt;If you are working with a library, or even your own project, make sure to mark files that have side effects in your package.json. This helps the bundler understand what can be safely tree-shaken.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"sideEffects"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="s2"&gt;"*.css"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="s2"&gt;"*.scss"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Tools to Analyze and Test Tree Shaking:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Webpack Bundle Analyzer&lt;/strong&gt;: This tool helps you visualize the contents of your bundle and see if any unused code is still being included.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Source Maps&lt;/strong&gt;: Use source maps to trace what’s being included in the final bundle.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Thoughts to take:
&lt;/h2&gt;

&lt;p&gt;Tree shaking is an essential optimization technique for React (and not only) developers who want to build efficient and fast applications. By following best practices such as using ES Modules, importing only necessary code, and ensuring modules are side-effect-free, you can take full advantage of tree shaking and significantly improve the performance of your React apps.&lt;/p&gt;

&lt;p&gt;If you’re looking to improve your app's performance, implementing tree shaking is a great place to start!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>npm</category>
      <category>javascript</category>
    </item>
    <item>
      <title>External libraries: The Hidden Weight of External Libraries</title>
      <dc:creator>Ion Bragaru</dc:creator>
      <pubDate>Wed, 11 Dec 2024 10:17:31 +0000</pubDate>
      <link>https://forem.com/bragaru-i/external-librariesthe-hidden-weight-of-external-libraries-abo</link>
      <guid>https://forem.com/bragaru-i/external-librariesthe-hidden-weight-of-external-libraries-abo</guid>
      <description>&lt;p&gt;As developers, we often rely on &lt;strong&gt;external hook libraries&lt;/strong&gt; to save time, leverage well-tested solutions, and focus on the bigger picture of our projects. However, it’s crucial to consider the impact these libraries have on your &lt;strong&gt;bundle size&lt;/strong&gt;—a key factor in your app’s performance and loading speed. Let’s explore how these libraries impact bundle size, how to check if tree-shaking is supported, and how to make informed decisions.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Bundle Size Matters
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;User Experience&lt;/strong&gt;: Larger bundles take longer to download, parse, and execute, especially on slower networks or devices.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SEO and Performance Scores&lt;/strong&gt;: Tools like Google Lighthouse penalize heavy bundles, impacting your search rankings.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Long-Term Maintenance&lt;/strong&gt;: Larger bundles can obscure performance bottlenecks as your project grows.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  External Hook Libraries: Convenience vs. Cost
&lt;/h2&gt;

&lt;p&gt;Hook libraries are a common solution for handling complex state or reusable patterns, but their bundle cost depends on their structure:&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Granular (Modular)&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Install only the hooks you need, keeping dependencies minimal.
&lt;/li&gt;
&lt;li&gt;Example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useDebounce&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hook-lib/useDebounce&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Monolithic (Tree-Shakable)&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Install one library, but ensure your build tool removes unused exports.
&lt;/li&gt;
&lt;li&gt;Example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useDebounce&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hook-lib&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each approach has trade-offs. Granular libraries offer precise control over what’s added, while monolithic libraries are easier to manage but require proper tree-shaking to avoid bloat.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Much Weight Do Hook Libraries Add?
&lt;/h2&gt;

&lt;p&gt;The weight depends on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Library Size&lt;/strong&gt;: Some libraries are lightweight (a few KB), while others can balloon to dozens of KB if they rely on dependencies.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tree-Shaking Effectiveness&lt;/strong&gt;: If the library doesn’t support tree-shaking, you might import unused code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Usage&lt;/strong&gt;: Importing a single hook might pull in shared utilities or polyfills, inflating the size.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example Scenario:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A lightweight library (use-fetch-hook) adds 5KB.&lt;/li&gt;
&lt;li&gt;A large, monolithic library with poor tree-shaking might add 30KB+, even if you only use one hook.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to Check if a Library Supports Tree-Shaking
&lt;/h2&gt;

&lt;p&gt;To check if a library supports tree-shaking, you can follow several approaches based on understanding its code structure and how it's bundled. Tree-shaking is a feature supported by modern JavaScript bundlers like &lt;code&gt;Webpack&lt;/code&gt; and &lt;code&gt;Rollup&lt;/code&gt;, which removes unused code during the build process. Here’s how you can determine if a library supports it:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Check the Library’s Package Documentation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Look for ES Module (ESM) Support&lt;/strong&gt;: For tree-shaking to work, the library must use ES Modules (ESM). ESM allows the bundler to analyze the import/export structure and safely eliminate unused code.

&lt;ul&gt;
&lt;li&gt;Check if the library provides an ESM build (often specified in the &lt;code&gt;module&lt;/code&gt; or &lt;code&gt;exports&lt;/code&gt; field of its &lt;code&gt;package.json&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Search the documentation or repository to see if ESM is mentioned as the preferred usage.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Check the &lt;code&gt;package.json&lt;/code&gt; of the Library
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Exports Field&lt;/strong&gt;: For more recent packages, check if the exports field is used. This can specify different entry points for different environments (like CommonJS or ESM), improving tree-shaking support.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Module Field&lt;/strong&gt;: Look at the &lt;code&gt;package.json&lt;/code&gt; file of the library. If it includes a module field that points to an ESM build, it indicates the library is compatible with tree-shaking. Example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"module"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"dist/library.esm.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"dist/library.cjs.js"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;module&lt;/code&gt; points to the ESM version, which is tree-shakable.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;main&lt;/code&gt; typically points to the CommonJS version, which isn’t ideal for tree-shaking. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3.Check the Library’s Source Code
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Use of &lt;code&gt;import/export&lt;/code&gt;:&lt;/strong&gt; Ensure that the library uses ES module syntax (e.g., import and export). Tree-shaking works best with this syntax.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the library uses CommonJS (require, module.exports), tree-shaking won’t be as effective.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;No Side Effects:&lt;/strong&gt; Libraries that support tree-shaking typically avoid side effects in their code. Check the library’s source code to ensure that functions or modules don’t perform actions when they are imported. For example, importing a module should not alter global state.&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Use a Bundler to Test Tree-Shaking
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You can use a modern JavaScript bundler (like Webpack or Rollup) to test if tree-shaking works. Here's a simple test:

&lt;ul&gt;
&lt;li&gt;Create a minimal project with the library installed.&lt;/li&gt;
&lt;li&gt;Import only a part of the library in your code (e.g., a single function).&lt;/li&gt;
&lt;li&gt;Run the bundler and check the output:&lt;/li&gt;
&lt;li&gt;a) If the unused code is excluded from the final bundle, the library supports tree-shaking.&lt;/li&gt;
&lt;li&gt;b) If the unused code is still included, then the library either doesn’t support tree-shaking or requires further configuration (like marking certain code as side-effect-free).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  5. Use a Bundle Analyzer
&lt;/h3&gt;

&lt;p&gt;Use tools like &lt;code&gt;Webpack Bundle Analyzer&lt;/code&gt; or &lt;code&gt;Rollup's built-in analyzer&lt;/code&gt; to visualize the final bundle.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Look for the size of the library in the output. If tree-shaking works, unused code should be excluded, and the final size should be smaller.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  6. Check the Community and Issues
&lt;/h3&gt;

&lt;p&gt;Look at issues or discussions in the library’s repository (e.g., GitHub) to see if there are any mentions of tree-shaking or issues related to it. The maintainers may also provide guidance on enabling tree-shaking.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. Look for Specific Build Instructions
&lt;/h3&gt;

&lt;p&gt;Some libraries might have specific instructions for enabling tree-shaking, especially when they are not entirely tree-shakable by default. Check for any guidance on how to configure the bundler for optimal tree-shaking.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you are using a library like Lodash, it has specific "modular" imports:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;debounce&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lodash&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This allows bundlers like Webpack to shake off unused methods when using Lodash's modular imports, as opposed to importing the entire library (import _ from 'lodash'), which would include the entire codebase and prevent tree-shaking.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>programming</category>
      <category>npm</category>
    </item>
    <item>
      <title>Create animations like PowerPoint slides in React with framer-motion and react-intersection-observe</title>
      <dc:creator>Ion Bragaru</dc:creator>
      <pubDate>Wed, 26 May 2021 09:11:09 +0000</pubDate>
      <link>https://forem.com/bragaru-i/create-animations-like-powerpoint-slides-in-react-with-framer-motion-and-react-intersection-observe-2bgf</link>
      <guid>https://forem.com/bragaru-i/create-animations-like-powerpoint-slides-in-react-with-framer-motion-and-react-intersection-observe-2bgf</guid>
      <description>&lt;h2&gt;
  
  
  Well, I’m a little bit far from use of animation libraries and kind of, but at my last project we had such a request:
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;Clients want a webpage that will have similar effects as a presentation in PowerPoint. On mousewheel we change our slides (previous and next)&lt;/li&gt;
&lt;li&gt;This PowerPoint effects should be available only on a desktop view (viewport 1200px and up). But for 1200px and down we must have a normal scrollable page.&lt;/li&gt;
&lt;li&gt;Time resource — critical low(well as always because need in some days to be in a production)&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;Do not to waste your time, check out final codesandbox here:&lt;br&gt;
&lt;a href="https://codesandbox.io/s/motion-framer-sample-3-l2y9x?file=/src/App.js:0-1912" rel="noopener noreferrer"&gt;***https://codesandbox.io/s/motion-framer-sample-3-l2y9x?file=/src/App.js:0-1912&lt;/a&gt;***&lt;/p&gt;

&lt;p&gt;Assuming all these criterias, the only option is to use a library with a good documentation( no need to reinvent the wheel) and a ready CRA init project. After CRA install, we add these libraries:&lt;br&gt;
 npm i framer-motion react-intersection-observer lodash&lt;/p&gt;

&lt;p&gt;Quick review on this libs:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Framer-Motion&lt;/strong&gt; — actually the package that helps us to animate our react components. API here: &lt;a href="https://www.framer.com/api/motion/" rel="noopener noreferrer"&gt;https://www.framer.com/api/motion/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;*&lt;em&gt;react-intersection-observer *&lt;/em&gt;— react implementation of the Intersection Observer API to tell you *when an element enters or leaves the viewport *(from official description). Docs here: &lt;a href="https://github.com/thebuilder/react-intersection-observer#readme" rel="noopener noreferrer"&gt;https://github.com/thebuilder/react-intersection-observer#readme&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;famous &lt;strong&gt;lodash **— well, we need only **debounce&lt;/strong&gt; function . Debounce description from official docs : &lt;em&gt;Creates a debounced function that delays invoking func until after wait milliseconds have elapsed since the last time the debounced function was invoked.&lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, we start to count steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create Sections — Slides. Giving them a width and height of 100%, also setting them id. id must be different for all sections.
our App.js look like:
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;em&gt;styles.css&lt;/em&gt; looks like that:

&lt;ul&gt;
&lt;li&gt;we reset margin and borders for all elements (&lt;em&gt;margin:0; padding:0&lt;/em&gt;) and create sections with a width and height of 100% of container(container is &lt;em&gt;100vh and 100vw from viewport&lt;/em&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;we create we flex center text and add some font size to text .&lt;/li&gt;
&lt;li&gt;some color to each section individually
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

Each section looks like that :&lt;/li&gt;
&lt;li&gt;nothing special, just a section tag with an ID (later we will add actions using this id! so important to each section to add a different id.
&lt;em&gt;./sections/Section1.jsx (all sections contains same code)&lt;/em&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

Now, we have sections, we have our packages setted up, let’s do some coding!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Every page has 4 effects:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Animation that is played when section is on viewport( &lt;strong&gt;&lt;em&gt;visible&lt;/em&gt;&lt;/strong&gt; )&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Animation that is played when user is leaving outport(&lt;strong&gt;&lt;em&gt;hidden&lt;/em&gt;&lt;/strong&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If user goes next section, we play exit animation for next section (&lt;strong&gt;&lt;em&gt;next&lt;/em&gt;&lt;/strong&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If user returns back to previous section, we play different exit animation (&lt;strong&gt;&lt;em&gt;previous&lt;/em&gt;&lt;/strong&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Well, let’s implement a custom hook that does all these. Also a file that constains all our constants ( delay! of course… we need a delay for our debounce function , that will optimize our &lt;strong&gt;&lt;em&gt;wheel&lt;/em&gt;&lt;/strong&gt; event listener)&lt;/p&gt;

&lt;p&gt;&lt;em&gt;./constants.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
*./useObserverHook.jsx&lt;br&gt;
 *So, what we did there:

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;we set by default &lt;strong&gt;&lt;em&gt;hidden&lt;/em&gt;&lt;/strong&gt; state of our animation, and if it is on viewport, with help of &lt;strong&gt;&lt;em&gt;useInView&lt;/em&gt;&lt;/strong&gt; hook we set state to &lt;em&gt;**visible&lt;/em&gt;&lt;em&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;useEffect&lt;/strong&gt;(() =&amp;gt; {&lt;br&gt;&lt;br&gt;
if (&lt;strong&gt;inView&lt;/strong&gt;) {&lt;br&gt;&lt;br&gt;
   &lt;em&gt;setVariant&lt;/em&gt;("&lt;strong&gt;visible&lt;/strong&gt;");&lt;br&gt;&lt;br&gt;
  } else setVariant("&lt;strong&gt;hidden&lt;/strong&gt;");&lt;br&gt;&lt;br&gt;
}, [&lt;strong&gt;inView&lt;/strong&gt;]);&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also we create a debounced function to trigger state change when user scrolls up or down, also we add a flag &lt;em&gt;isFirst&lt;/em&gt; and *isLast *(well you dont need to go on previous section if you are on last section and viceversa)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const ***onMouseScroll*** = **debounce**((e) =&amp;gt; {        
  if (e.deltaY &amp;gt; 0 &amp;amp;&amp;amp; variant === "**visible**") 
     setVariant(isLast ? "**visible**" : "**next**")        
  if (e.deltaY &amp;lt; 0 &amp;amp;&amp;amp; variant === "**visible**") 
     setVariant(isFirst ? "**visible**" : "**previous**")    
 }, MAIN_DELAY)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
A codesandbox with sample files:&lt;br&gt;&lt;br&gt;
&lt;iframe&gt;
  src="https://codesandbox.io/embed/motion-framer-sample-1-39iji"&lt;br&gt;
  style="width:100%; height:calc(300px + 8vw); border:0; border-radius: 4px; overflow:hidden;"&lt;br&gt;
  allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb"&lt;br&gt;
  loading="lazy"&lt;br&gt;
  sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"&amp;gt;&lt;br&gt;
&lt;/iframe&gt;&lt;br&gt;
&lt;br&gt;&lt;br&gt;
Now, let’s implement that hook on &lt;em&gt;Sections1.jsx&lt;/em&gt;!&lt;br&gt;&lt;/p&gt;

&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
— First, we’re gonna create a *./constants.js *file and export MAIN_DELAY constant we’re we declare it to 600ms (well, this actually is debounce value for our hook — we can infinite scroll, but event will be trigerred after last “wheel” effect )&lt;br&gt;&lt;br&gt;
 — Let’s console.log our sandbox, to be sure that we going on the right way:&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%2Frh4quwn95tdht0gah4hq.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%2Frh4quwn95tdht0gah4hq.gif" width="600" height="555"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;— Now we have variants for triggering animation on mouse wheel , so next step we will do that. To do that we need to import motion from motion-framer and wrap our sections with &lt;em&gt;motion. *Well, we got something like that:&lt;br&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
To create these PowerPoint*ish slide effects, we need to create a separate div that will be positionated absolute on top of a section, and with different variants of animations, so we update our *./styles.css *with this block of code:&lt;br&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
So, when user use wheel down event, we play for drawer — *drawerVar&lt;/em&gt;, and for section — &lt;em&gt;sectionVar. *Code sandbox here:&lt;br&gt;
&lt;iframe src="https://codesandbox.io/embed/motion-framer-sample-2-hpsqs"&gt;
&lt;/iframe&gt;
&lt;br&gt;
And last small step, is just to switch our pages/slides on same mouse wheel event.&lt;br&gt;
To do that we need to update our *App.jsx&lt;/em&gt; component (navigation — on scroll we switch out the slide and leave 0.4s to play animations for sections):&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
And some small updates on sections, and we get this effects :&lt;br&gt;
a) for drawer — previous and next&lt;br&gt;
b) for content — hidden and visible

&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%2Fm5muktfyjs3haur6io5l.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%2Fm5muktfyjs3haur6io5l.gif" width="600" height="581"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Final results on codesandbox here:&lt;br&gt;
&lt;a href="https://codesandbox.io/s/motion-framer-sample-3-l2y9x?file=/src/App.js:0-1912" rel="noopener noreferrer"&gt;***https://codesandbox.io/s/motion-framer-sample-3-l2y9x?file=/src/App.js:0-1912&lt;/a&gt;***&lt;br&gt;
&lt;iframe src="https://codesandbox.io/embed/motion-framer-sample-2-hpsqs"&gt;
&lt;/iframe&gt;
&amp;gt;  &lt;strong&gt;&lt;em&gt;Happy Coding!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
