<?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: Sreashi Saha</title>
    <description>The latest articles on Forem by Sreashi Saha (@sreashi).</description>
    <link>https://forem.com/sreashi</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%2F635065%2F7e5df417-5e86-4737-906c-a325869246c6.jpeg</url>
      <title>Forem: Sreashi Saha</title>
      <link>https://forem.com/sreashi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sreashi"/>
    <language>en</language>
    <item>
      <title>React Animations Using Framer Motion</title>
      <dc:creator>Sreashi Saha</dc:creator>
      <pubDate>Sun, 25 Jul 2021 17:26:32 +0000</pubDate>
      <link>https://forem.com/sreashi/react-animations-using-framer-motion-eg2</link>
      <guid>https://forem.com/sreashi/react-animations-using-framer-motion-eg2</guid>
      <description>&lt;p&gt;Before Knowing about framer motion first we should know why should we need Framer motion? Framer motion &lt;strong&gt;improves upon and simplifies&lt;/strong&gt; the API in a way that couldn't have been done without breaking changes and rewriting. As a react developer, I found it very exciting as I can create animations using technologies that I’m already familiar with.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Framer Motion?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Framer Motion is a production-ready motion library to create animations using React. It brings declarative animations, effortless layout transitions, and gestures while maintaining HTML and SVG semantics.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to use Framer Motion in our Project&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Install framer motion using in your project using&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 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember one thing &lt;em&gt;Framer-Motion requires that you are using React version 16.8 or higher&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Once installed, you can import Motion into your components via framer-motion.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { motion } from "framer-motion"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s it. Now you are able to use framer-motion in your Project. Let's see basic syntaxes of Framer Motion with few examples.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Animation&lt;/strong&gt;&lt;br&gt;
Animations are driven by Framer Motion’s versatile animate prop. It can cater to the very simplest, and most advanced use-cases.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Motion components are one of the core elements in Framer Motion, and most animations in Framer Motion are controlled via the motion component’s flexible animate property.*&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Depending on your requirements, you can use the animate property in a variety of ways:&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;motion.div animate={{ x: 200,rotate:180 }} /&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;In the above example, the div tag will move 200px to the right and rotate 180 degrees.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Transition&lt;/strong&gt;&lt;br&gt;
This transition can be optionally configured using Popmotion’s familiar tween, spring, and inertia animations.&lt;/p&gt;

&lt;p&gt;Physical properties like x and scale are animated via spring by default, while values like opacity and color are animated with a tween. You can also use the transition prop to change properties like duration, delay, and stiffness of the animation.&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;motion.div animate={{ x: 200,rotate:180 }}   
transition={{ duration: 2, repeat: Infinity }} /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, the div tag will move 200px to the right and rotate 180 degrees. the duration and repeat props are used to keep that animation in a loop with a duration of 2 seconds.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Keyframes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Values in  animate can also be set as a series of keyframes. This will animate through each value in a sequence. By default, a keyframes animation will start with the first item in the array.&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;motion.div animate={{ scale: [2, 2, 1] }} /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, div will scale through each value in the sequence.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Variants&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Variants are a collection of pre-defined target objects passed into motion components by using the variants prop. Target objects are useful for simple, single-component animations. But sometimes we want to create animations that propagate throughout the DOM and orchestrate those animations in a declarative way. &lt;/p&gt;

&lt;p&gt;By defining variants on a set of components, providing a variant label to animate will propagate this animation through all the children like additional transition props such as when, delayChildren, and staggerChildren.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as React from "react";
import { motion } from "framer-motion";


    const variants = {
     open: {
      transition: { staggerChildren: 0.09, delayChildren: 0.3 }
     },
     closed: {
      transition: { staggerChildren: 0.06, staggerDirection: -1 }
     }
    };
    export const Navigation = () =&amp;gt; (
      &amp;lt;motion.ul variants={variants}&amp;gt;
        {itemIds.map(i =&amp;gt; (
          &amp;lt;MenuItem i={i} key={i} /&amp;gt;
        ))}
      &amp;lt;/motion.ul&amp;gt;
    );


const itemIds = [ 1, 2, 3, 4];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, staggerChildren and delayChildren props are used to delay the transition of the menu items. In addition, the staggerDirection prop is used to specify the direction of the stagger.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gestures&lt;/strong&gt;&lt;br&gt;
Motion extends the basic set of event listeners provided by React with a simple yet powerful set of UI gesture recognizers. It currently has support for hover, tap, pan, and drag gesture detection. Each gesture has a series of event listeners that you can attach to your motion component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hover&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The hover gesture detects when a pointer hovers over or leaves a component. There are three hover props available — whileHover, onHoverStart(event, info), and onHoverEnd(event, info).&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;motion.div
  whileHover={{ scale: 1.2 }}
  onHoverStart={() =&amp;gt; console.log("starts")}
  onHoverEnd={() =&amp;gt; console.log("ends")}

/&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;n the above example, when we will hover over the div its size will increase and when we start hovering in the console it will print 'start', and when we stop hovering it will print 'end'.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Focus&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The focus gesture detects when a component gains or loses focus through a click, focus, or by tabindex. the focus prop is whileFocus.&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;motion.div
whileFocus={{ scale: 1.5 }}
/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, when we will focus on the div its size will increase.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tap&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The tap gesture detects when a pointer presses down and releases on the same component.&lt;/p&gt;

&lt;p&gt;There are three  hover  props  available — whiletap,onTap(event,info),onTapStart(event, info),onTapCancel(event, info)&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;motion.div whileTap={{ scale: 1.2 }} /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, when we will Tap on the div its size will increase.&lt;br&gt;
&lt;strong&gt;Pan&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The pan gesture recognizes when a pointer presses down on a component and moves further than 3 pixels. The pan gesture is ended when the pointer is released. &lt;/p&gt;

&lt;p&gt;There are three  hover  props  available — onPanStart,onPan,onPanEnd&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For pan gestures to work correctly with touch input, the element needs touch scrolling to be disabled on either x/y or both axis with the touch-action CSS rule&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function onPan(event, info) {
  console.log(info.point.x, info.point.y)
}


&amp;lt;motion.div onPan={onPan} /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, A Info object containing x and y values for point is relative to the device or page. delta is Distance moved since the last event. offset is  Offset from the original pan event. velocity is the Current velocity of the pointer.&lt;br&gt;
&lt;strong&gt;Drag&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The drag gesture follows the rules of the pan gesture but applies pointer movement to the x and/or y-axis of the component.&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;motion.div
  drag
  dragTransition={{
    min: 0,
    max: 100,
    bounceStiffness: 100
  }}
&amp;gt;
 Drag 

&amp;lt;/motion.div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, dragging is enable for both x-direction and y-directions. If you want to limit it only to the x-direction, you can set the drag property value to “x”: drag=“x”.&lt;/p&gt;

&lt;p&gt;Hopefully, It helps you to start using framer motion in your React project for building some really cool animations. If any queries regarding this write them down in the comment below. Thank you for your time and I hoped my blog is helpful for you.&lt;/p&gt;

&lt;p&gt;Happy Learning!!&lt;/p&gt;

</description>
      <category>react</category>
      <category>framermotion</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>React Code Splitting</title>
      <dc:creator>Sreashi Saha</dc:creator>
      <pubDate>Sun, 04 Jul 2021 20:24:40 +0000</pubDate>
      <link>https://forem.com/sreashi/react-code-splitting-3cap</link>
      <guid>https://forem.com/sreashi/react-code-splitting-3cap</guid>
      <description>&lt;p&gt;Before Knowing about Code Splitting we need to know Why we need Code Splitting these first we need to know about bundling. Bundling is a process that takes multiple files and merges them into a single file, which is called a bundle. Most React apps will have their files “bundled” using tools like Webpack, Rollup, or Browser. Let's see an example:&lt;br&gt;
&lt;strong&gt;App:&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 { add } from './math.js'; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





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

export function add(a, b) {

  return a + b;
}


console.log(add(2, 4)); // 6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Bundle:&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;function add(a, b) {
  return a + b;
}


console.log(add(2,4)); // 6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As our app grows, our bundle will grow, especially when using large third-party libraries. If the bundle size gets large, it takes a long time to load on a webpage. Resolving these issues Code splitting comes into the scenario.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Splitting&lt;/strong&gt;&lt;br&gt;
Code-Splitting is a feature supported by Webpack and Browserify, which can create multiple bundles that can be dynamically loaded at runtime. Code splitting your app can help you “lazy-load” just the things that are currently needed by the user.&lt;br&gt;
&lt;strong&gt;Features&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The code splitting improves the performance of the app&lt;/li&gt;
&lt;li&gt;The code splitting improves the impact on memory&lt;/li&gt;
&lt;li&gt;The code splitting improves the downloaded Kilobytes (or Megabytes) size&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;import()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The best way to introduce code-splitting into your app is through the dynamic import(). When Webpack comes across this syntax, it automatically starts code-splitting your app. If you’re using Create React App, this is already configured for you and you can start using it immediately. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Before:&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 { sub } from './math';

console.log(sub(20, 10));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;After:&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("./math").then(math =&amp;gt; {
  console.log(math.sub(20, 10));
});

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;React.lazy:&lt;/strong&gt;&lt;br&gt;
The React.lazy function lets you render a dynamic import as a regular component.React.lazy takes a function that must call a dynamic import(). This must return a Promise which resolves to a module with a default export containing a React component. React.lazy and Suspense are not yet available for server-side rendering.&lt;br&gt;
&lt;strong&gt;Before:&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 OtherComponent from './ExampleComponent';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const OtherComponent = React.lazy(() =&amp;gt; import('./ExampleComponent'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will automatically load the bundle containing the ExampleComponent when this component is first rendered.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;React.lazy components are not yet available for server-side rendering. For code-splitting in a server-rendered app, it is recommended to use Loadable Components.&lt;/code&gt;&lt;/p&gt;

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

&lt;p&gt;If the module which contains the ExampleComponent is not yet loaded by the function component(MyComponent), then the lazy component should then be rendered inside a Suspense component, which allows us to show some fallback content while we’re waiting for the lazy component to load.&lt;/p&gt;

&lt;p&gt;*The fallback prop accepts any React elements that you want to render while waiting for the component to load.&lt;/p&gt;

&lt;p&gt;*You can place the Suspense component anywhere above the lazy component.&lt;/p&gt;

&lt;p&gt;*You can even wrap multiple lazy components with a single Suspense component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Before:&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, { Suspense } from 'react';

const OtherComponent = React.lazy(() =&amp;gt; import('./OtherComponent'));

function MyComponent() {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;Suspense fallback={&amp;lt;div&amp;gt;Loading...&amp;lt;/div&amp;gt;}&amp;gt;
        &amp;lt;OtherComponent /&amp;gt;
      &amp;lt;/Suspense&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;After:&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, { Suspense } from 'react';

const OtherComponent = React.lazy(() =&amp;gt; import('./OtherComponent'));
const AnotherComponent = React.lazy(() =&amp;gt; import('./AnotherComponent'));

function MyComponent() {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;Suspense fallback={&amp;lt;div&amp;gt;Loading...&amp;lt;/div&amp;gt;}&amp;gt;
        &amp;lt;section&amp;gt;
          &amp;lt;OtherComponent /&amp;gt;
          &amp;lt;AnotherComponent /&amp;gt;
        &amp;lt;/section&amp;gt;
      &amp;lt;/Suspense&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Suspense components are not yet available for server-side rendering. For code-splitting in a server-rendered app, it is recommended to use Loadable Components.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Error boundaries:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If any module fails to load, for example, due to network failure, we will get an error that can handle these errors with Error Boundaries. Once we have created the Error Boundary, we can use it anywhere above our lazy components to display an error state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import MyErrorBoundary from './MyErrorBoundary';  
const ExampleComponent = React.lazy(() =&amp;gt; import('./ ExampleComponent'));  
const ExamComponent = React.lazy(() =&amp;gt; import('./ ExamComponent'));  

const MyComponent = () =&amp;gt; (  
  &amp;lt;div&amp;gt;  
    &amp;lt;MyErrorBoundary&amp;gt;  
      &amp;lt;Suspense fallback={&amp;lt;div&amp;gt;Loading...&amp;lt;/div&amp;gt;}&amp;gt;  
        &amp;lt;section&amp;gt;  
          &amp;lt;ExampleComponent /&amp;gt;  
          &amp;lt;ExamComponent /&amp;gt;  
        &amp;lt;/section&amp;gt;  
      &amp;lt;/Suspense&amp;gt;  
    &amp;lt;/MyErrorBoundary&amp;gt;  
  &amp;lt;/div&amp;gt;  
);  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Route-based code splitting:&lt;/strong&gt;&lt;br&gt;
Deciding where in your app to introduce code splitting can be a bit tricky. You want to make sure you choose places that will split bundles evenly but won’t disrupt the user experience.&lt;/p&gt;

&lt;p&gt;A good place to start is with routes. Most people on the web are used to page transitions taking some amount of time to load. You also tend to be re-rendering the entire page at once so your users are unlikely to be interacting with other elements on the page at the same time.&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, { Suspense, lazy } from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';

const Home = lazy(() =&amp;gt; import('./routes/Home'));
const About = lazy(() =&amp;gt; import('./routes/About'));

const App = () =&amp;gt; (
  &amp;lt;Router&amp;gt;
    &amp;lt;Suspense fallback={&amp;lt;div&amp;gt;Loading...&amp;lt;/div&amp;gt;}&amp;gt;
      &amp;lt;Switch&amp;gt;
        &amp;lt;Route exact path="/" component={Home}/&amp;gt;
        &amp;lt;Route path="/about" component={About}/&amp;gt;
      &amp;lt;/Switch&amp;gt;
    &amp;lt;/Suspense&amp;gt;
  &amp;lt;/Router&amp;gt;
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Named Exports:&lt;/strong&gt;&lt;br&gt;
React.lazy currently supports only default exports. An intermediate module that re-exports as default has to be created if one wants to import a module that uses named exports. This ensures the working of tree shaking and prevents the pulling in of unused components.&lt;br&gt;
&lt;strong&gt;Components.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;export const Component = /* ... */;
export const MyUnusedComponent = /* ... */;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Component.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;export { Component as default } from "./Components.js";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;MyApp.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;import {React, lazy} from 'react';
const Component = lazy(() =&amp;gt; import("./Component.js"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hopefully, Code Splitting is a bit clear by now. It helps to improve your efficiency in writing React. If any queries regarding this write them down in the comment below. Thank you for your time and I hoped my blog is helpful for you.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>keeplearning</category>
    </item>
    <item>
      <title>Accessibility in React</title>
      <dc:creator>Sreashi Saha</dc:creator>
      <pubDate>Wed, 23 Jun 2021 09:52:32 +0000</pubDate>
      <link>https://forem.com/sreashi/accessibility-in-react-2m2n</link>
      <guid>https://forem.com/sreashi/accessibility-in-react-2m2n</guid>
      <description>&lt;p&gt;Accessibility is the ability for applications to be used by everyone, including those with disabilities.As a devloper its your responsibility to make sure that everyone get the right experience in your app. For making your react app Accessible you should know what is accessibility exactly is??  so let's get started&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Accessibility and Why it is needed?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Web accessibility is also referred to as a11y.A11y is a numero-nym that stands for accessibility as “a” followed by 11 more letters, followed by “y”.It is the design and creation of websites that can be used by everyone.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Accessibility support is necessary to allow assistive technology to interpret web pages.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Accessibility is the necessary tool or ways in which a website can be made easy to access by the user with features like clickable buttons or drop downs or spaces to write a comment and stuff.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Accessibility also helps in power users may find it faster to interact with your application using a keyboard, rather than a mouse or touch screen. Especially for applications involving a large amount of data entry, good keyboard navigation support can dramatically increase user productivity.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Accessibility Standards and Guidelines&lt;/strong&gt;&lt;br&gt;
There are some standard guidelines available for achieving accessibility which helps us to achive accessibility for building websites.Some of them are:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WCAG:&lt;/strong&gt; Web Content Accessibility Guidelines (WCAG) is developed through the W3C process in cooperation with individuals and organizations around the world, with a goal of providing a single shared standard for web content accessibility that meets the needs of individuals, organizations, and governments internationally.The WCAG documents explain how to make web content more accessible to people with disabilities. Web “content” generally refers to the information in a web page or web application, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;natural information such as text, images, and sounds&lt;/li&gt;
&lt;li&gt;code or markup that defines structure, presentation, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;WAI-ARIA:&lt;/strong&gt; Web Accessibility Initiative - Accessible Rich Internet Applications (WAI-ARIA) is the Accessible Rich Internet Applications Suite, defines a way to make Web content and Web applications more accessible to people with disabilities. It especially helps with dynamic content and advanced user interface controls developed with HTML, JavaScript, and related technologies.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Accessibility Using React:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;React fully supports building accessible websites, often by using standard HTML techniques.Let's see how we can make your react applications more accessible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ARIA:&lt;/strong&gt;&lt;br&gt;
Accessible Rich Internet Applications (ARIA) is a set of attributes that define ways to make Web content and Web applications more accessible for users with disabilities.ARIA  HTML attributes are fully supported in JSX, and therefore can be used as attributes for elements and components in React. Whereas most DOM properties and attributes in React are camelCased, these attributes should be hyphen-cased.&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;input
  aria-label={labelText}
  aria-required="true"
  onChange={onchangeHandler}
  value={inputValue}
  name="name"
/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Semantic HTML:&lt;/strong&gt;&lt;br&gt;
Semantic HTML is the foundation of accessibility in a web application.Sometimes we break HTML semantics when we add &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; elements to our JSX to make our React code work, especially when working with lists &lt;code&gt;(&amp;lt;ol&amp;gt;, &amp;lt;ul&amp;gt; and &amp;lt;dl&amp;gt;)&lt;/code&gt; and the HTML &lt;code&gt;&amp;lt;table&amp;gt;&lt;/code&gt; this  makes a problem of understanding the code and thus debugging the code.&lt;/p&gt;

&lt;p&gt;So at times, we use pieces like &amp;lt;&amp;gt; or   allows you to group together a list of child elements without adding more nodes to the DOM.&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, { Fragment } from 'react';
function  employeeList() {
return (
    &amp;lt;Fragment&amp;gt;  
        &amp;lt;dt&amp;gt;EA824412&amp;lt;/dt&amp;gt;
        &amp;lt;dd&amp;gt; Sreashi Saha&amp;lt;/dd&amp;gt;
    &amp;lt;/Fragment&amp;gt; );
}

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Labeling:&lt;/strong&gt;&lt;br&gt;
Labeling is used to makes the forms accesible.Every HTML form control, such as &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;textarea&amp;gt;&lt;/code&gt;, needs to be labeled accessibly. There is one important thing you must provide in your application: a textual label for each control . This gives a screen reader user context about the control they are interacting with.Although these standard HTML practices can be directly used in React, note that the for attribute is written as htmlFor in JSX:&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;label htmlFor="name"&amp;gt;Name:&amp;lt;/label&amp;gt;
&amp;lt;input id="name" type="text" name="name"/&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Keyboard Focus:&lt;/strong&gt;&lt;br&gt;
Keyboard Focus refers to the part of the web application that is accepting data or actions from the keyboard from the user often refers to the DOM input.We can use ref functions to handle where we want the user to focus on in our application.Using the React.createRef() we can control the focus.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use the &lt;code&gt;ref&lt;/code&gt; callback to store a reference to the text input DOM&lt;/li&gt;
&lt;li&gt;Create a ref to store the textInput DOM element&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's have a look how to create a ref to an element in the JSX of a component class..&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class CustomDiv extends React.Component {
  constructor(props) {
    super(props);

    this.div = React.createRef();
  }
  render() {

    return (
      &amp;lt;div
        tabIndex= "-1"

        ref={this.div}
      /&amp;gt;
    );
  }


}

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

&lt;/div&gt;



&lt;p&gt;Then we can focus it elsewhere in our component when needed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Explicitly focus the div using raw DOM API&lt;/li&gt;
&lt;li&gt;We are accessing "current" to get the DOM node
&lt;/li&gt;
&lt;/ul&gt;

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

 this.textInput.current.focus();


}

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

&lt;/div&gt;



&lt;p&gt;When using a HOC to extend components, it is recommended to foward the ref to the wrapped component using the forwardRef function of React. If a third party HOC does not implement ref forwarding, the above pattern can still be used as a fallback.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting Document Title:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Setting document title  provides a declarative way to specify document.title in a single-page app.It is actually crucial for screen readers, the document title is the first thing screen readers announce. It updates the content currently showing in the browser tab helping to announce exactly where the users &lt;code&gt;(who might depend on screen readers)&lt;/code&gt; are in your application. It is also really great for search engine optimisation.Set the document &lt;code&gt;&amp;lt;title&amp;gt;&lt;/code&gt; to correctly describe the current page content as this ensures that the user remains aware of the current page context.Let's see 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;function App() {
  return (
    &amp;lt;DocumentTitle title='My Web App'&amp;gt;
      &amp;lt;SomeRouter /&amp;gt;
    &amp;lt;/DocumentTitle&amp;gt;
  );
}

class NewArticlePage extends React.Component {
  constructor(props) {
    super(props);
    this.state = { title: 'Untitled' };
  }

  render() {
    return (
      &amp;lt;DocumentTitle title={this.state.title}&amp;gt;
        &amp;lt;div&amp;gt;
          &amp;lt;h1&amp;gt;New Article&amp;lt;/h1&amp;gt;
          &amp;lt;input
            value={this.state.title}
            onChange={(e) =&amp;gt; this.setState({ title: e.target.value })}
          /&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/DocumentTitle&amp;gt;
    );
  }

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Development Assistance:&lt;/strong&gt;&lt;br&gt;
Devlopment Assistance can helps to improve yours  website Accessibility.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install ESLint jsx-a11y plugin for your React projects to display accessibility rules you miss while building your application.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install eslint-plugin-jsx-a11y  -— save-dev

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Update your eslint.rc file’s plugin and extends sections in your code editor. For plugin section:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "extends": ["react-app", "plugin:jsx-a11y/recommended"],
  "plugins": ["jsx-a11y"]

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

&lt;/div&gt;



&lt;p&gt;Hopefully, It helps to improve your efficiency writing React and also helps to build your website more accesible. If any queries regarding this write them down in the comment below. Thank you for your time and I hoped my blog is helpful for you.&lt;/p&gt;

&lt;p&gt;Happy Learning!!&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>a11y</category>
    </item>
    <item>
      <title>ReactDOM</title>
      <dc:creator>Sreashi Saha</dc:creator>
      <pubDate>Sat, 05 Jun 2021 13:45:43 +0000</pubDate>
      <link>https://forem.com/sreashi/reactdom-494g</link>
      <guid>https://forem.com/sreashi/reactdom-494g</guid>
      <description>&lt;p&gt;Before knowing about ReactDom first we should know about What DOM is?? So Dom is a Document Object Model, a tree-like structure that contains all the elements and properties of a website as its nodes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is React-Dom??&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The react-dom package provides DOM-specific methods that can be used at the top level of your app to enable an efficient way of managing DOM elements of the webpage.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to import React-Dom?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To use the ReactDOM in any React web app we must first import ReactDOM from the react-dom packages by the following syntax.&lt;/p&gt;

&lt;p&gt;ES6 - import ReactDOM from 'react-dom'&lt;br&gt;
ES5-var ReactDOM = require('react-dom')&lt;br&gt;
ReactDOM provides the developers with an API containing the following methods.&lt;/p&gt;

&lt;p&gt;*render()&lt;br&gt;
 *hydrate()&lt;br&gt;
 *unmountComponentAtNode()&lt;br&gt;
 *findDOMNode()&lt;br&gt;
 *createPortal()&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;render( )&lt;/strong&gt; :  render() is used to render a single React Component or several Components wrapped together in a Component or a div element.&lt;/p&gt;

&lt;p&gt;Syntax:&lt;/p&gt;

&lt;p&gt;ReactDOM.render(element, container, callback)&lt;/p&gt;

&lt;p&gt;Render a React element into the DOM in the supplied container.&lt;br&gt;
If the React element was previously rendered into container, this will perform an update on it.&lt;/p&gt;

&lt;p&gt;If the optional callback is provided, it will be executed after the component is rendered or updated.&lt;/p&gt;

&lt;p&gt;return a reference to the component or returns null for stateless components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;hydrate()&lt;/strong&gt;: hydrate() is same as render(), but is used to hydrate a container whose HTML contents were rendered by ReactDOMServer. React will attempt to attach event listeners to the existing markup.&lt;/p&gt;

&lt;p&gt;Syntax:&lt;/p&gt;

&lt;p&gt;ReactDOM.hydrate(element, container, callback)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;unmountComponentAtNode()&lt;/strong&gt; : unmountComponentAtNode() is remove a mounted React component from the DOM and clean up its event handlers and state.&lt;/p&gt;

&lt;p&gt;If no component was mounted in the container, calling this function does nothing. Returns true if a component was unmounted and false if there was no component to unmount.&lt;/p&gt;

&lt;p&gt;Syntax:&lt;/p&gt;

&lt;p&gt;ReactDOM.unmountComponentAtNode(container)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;findDOMNode()&lt;/strong&gt;: This function is generally used to get the DOM node, this returns the corresponding native browser DOM element. This method is useful for reading values out of the DOM, such as form field values and performing DOM measurements. In most cases,&lt;/p&gt;

&lt;p&gt;you can attach a ref to the DOM node and avoid using findDOMNode at all&lt;/p&gt;

&lt;p&gt;findDOMNode is an escape hatch used to access the underlying DOM node.&lt;/p&gt;

&lt;p&gt;In most cases, the use of this escape hatch is discouraged because it pierces the component abstraction. It has been deprecated in StrictMode.&lt;br&gt;
This method takes a single parameter component that expects a React Component to be searched in the Browser DOM.&lt;/p&gt;

&lt;p&gt;Syntax:&lt;/p&gt;

&lt;p&gt;ReactDOM.findDOMNode(component)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;createPortal()&lt;/strong&gt;: Portals provide a way to render children into a dom node that exists outside the hierarchy of the DOM component.&lt;/p&gt;

&lt;p&gt;when an element is returned from a component’s render method, it’s mounted on the DOM as a child of the nearest parent node which in some cases may not be desired.&lt;/p&gt;

&lt;p&gt;Syntax:&lt;/p&gt;

&lt;p&gt;ReactDOM.createPortal(child, container)&lt;/p&gt;

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

&lt;p&gt;ReactDOM uses observables thus provides an efficient way of DOM handling.&lt;/p&gt;

&lt;p&gt;ReactDOM can be used on both the client-side and server-side.&lt;/p&gt;

&lt;p&gt;Hopefully, ReactDOM is a bit clear by now. If any queries regarding this write them down below. Thank you for your time and I hoped my blog is helpful for you.&lt;/p&gt;

&lt;p&gt;Happy Learning!!&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>keeplearning</category>
      <category>javascript</category>
    </item>
    <item>
      <title>useState and useEffect in React</title>
      <dc:creator>Sreashi Saha</dc:creator>
      <pubDate>Sat, 29 May 2021 10:14:11 +0000</pubDate>
      <link>https://forem.com/sreashi/usestate-and-useeffect-in-react-287c</link>
      <guid>https://forem.com/sreashi/usestate-and-useeffect-in-react-287c</guid>
      <description>&lt;p&gt;In this blog, we will learn about useState and useEffeact in react hooks. Hooks are a new edition in React 16.8. React hooks provide an ability to use state and lifecycle functions within functional components.&lt;/p&gt;

&lt;p&gt;Before knowing about the two major hooks(i.e useState and useEffect) we need to keep some major rules regarding hooks in our mind.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Don’t call hooks from inside nested functions, loops, or conditionals.&lt;/li&gt;
&lt;li&gt;Don’t call hooks from a regular JavaScript function.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;useState&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The useState hook is a function that takes one argument, which is the initial state, and it returns two values: the current state and a function that can be used to update the state.&lt;/p&gt;

&lt;p&gt;let's see 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; import React, { useState } from 'react';

    function Example() {
      const [count, setCount] = useState(0);

      return (
        &amp;lt;div&amp;gt;
           &amp;lt;p&amp;gt;You clicked {count} times&amp;lt;/p&amp;gt;
           &amp;lt;button onClick={() =&amp;gt; setCount(count + 1)}&amp;gt;
            Click me
            &amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
      );
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;useState is a new way to use the exact same capabilities that this.state provides in a class.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;square brackets("[]") is a JavaScript syntax called "array destructing". It is used for It means that we’re making two new variables "count" and "setCount". &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The only argument to the useState is the initial state. So in the above example, "0" is the initial state for the variable count.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;useState returns an array of two values the current state value and the function/method that can be used to update the state. So in the above example, it returns two values count and setCount.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;useEffect&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;useEffect Hook is to eliminate the side-effects of using class-based components. This hook is the ideal place to set up listeners, fetching data from API and removing listeners before the component is removed from the DOM.&lt;/p&gt;

&lt;p&gt;The useEffect hook is the combination of componentDidMount(run only once when the component is inserted into the DOM tree structure) componentDidUpdate(run something on each render), and componentWillUnmount (run when the component will be removed from the DOM tree) class lifecycle methods. &lt;/p&gt;

&lt;p&gt;let's see 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;import React, { useState, useEffect } from 'react';

function Example() {
  const [count, setCount] = useState(0);

  useEffect(() =&amp;gt; {
    document.title = `You clicked ${count} times`;
  });

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;You clicked {count} times&amp;lt;/p&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; setCount(count + 1)}&amp;gt;
        Click me
      &amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;useEffect placing inside the component lets us access the count state variable (or any props) right from the effect.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;useEffect runs both after the first render and after every update.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;useEffect doesn’t block the browser from updating the screen.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So, we can say that hooks are added to React to use "state" within the functionals components instead of using classes.&lt;/p&gt;

&lt;p&gt;Hopefully, useState and useEffect is a bit clear by now. If any queries regarding this write them down below. Thank you for your time and I hoped my blog is helpful for you.&lt;/p&gt;

&lt;p&gt;Happy Learning!!&lt;/p&gt;

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