<?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: Nitsan Cohen</title>
    <description>The latest articles on Forem by Nitsan Cohen (@nitsancohen770).</description>
    <link>https://forem.com/nitsancohen770</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%2F628885%2Ff47d0938-2a4c-430d-a5db-b1b43548b3d7.jpg</url>
      <title>Forem: Nitsan Cohen</title>
      <link>https://forem.com/nitsancohen770</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nitsancohen770"/>
    <language>en</language>
    <item>
      <title>3 Ways to Create React Components with Bit</title>
      <dc:creator>Nitsan Cohen</dc:creator>
      <pubDate>Mon, 01 Jan 2024 17:24:22 +0000</pubDate>
      <link>https://forem.com/nitsancohen770/3-ways-to-create-react-components-with-bit-1o06</link>
      <guid>https://forem.com/nitsancohen770/3-ways-to-create-react-components-with-bit-1o06</guid>
      <description>&lt;p&gt;Bit breaks down large codebases into easy-to-understand and maintain Bit components. Each Bit component is maintained as an autonomous building block with its own dependencies, dev tools, and even source control.&lt;/p&gt;

&lt;p&gt;This blog will explore 3 ways to create a component with Bit. We'll use our framework of choice, though keep in mind Bit is not limited to any specific technology or framework. &lt;/p&gt;




&lt;h2&gt;
  
  
  Creating a React Component Using a Template
&lt;/h2&gt;

&lt;p&gt;Using &lt;a href="https://bit.dev/docs/getting-started/composing/creating-components"&gt;Bit Create&lt;/a&gt;, you can quickly generate a new &lt;a href="https://bit.dev/docs/react-components/react-components"&gt;React component&lt;/a&gt; using a &lt;a href="https://bit.dev/reference/generator/use-component-generator"&gt;component generator&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Component generators offer the basic boilerplate for the component's source files as well as the default configuration (in this example, the template configures the generated component to use the same reusable React development environment).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Command:&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;bit create react my-first-react-component
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;Speeds up the initial setup of React components.&lt;/li&gt;
&lt;li&gt;Maintain consistent development standards across developers collaborating on a project.&lt;/li&gt;
&lt;li&gt;Allows customization to fit specific project standards.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ol&gt;
&lt;li&gt;Explore available templates by running &lt;code&gt;bit templates&lt;/code&gt;:
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IrAgM1G9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n97u1gr0k6blm4m84brj.png" alt="Image description" width="800" height="418"&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Choose the React template from the organization's available templates.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run the command to create a new React component:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bit create react my-first-react-component
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The React component template generated these files.&lt;/p&gt;




&lt;h2&gt;
  
  
  Creating a Bit Component by Tracking an Existing Directory
&lt;/h2&gt;

&lt;p&gt;This method is ideal for those gradually &lt;a href="https://bit.dev/blog/build-bit-components-in-your-existing-project-li2884pg"&gt;adopting Bit in existing projects&lt;/a&gt;. It enables tracking and maintaining existing directories as Bit components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Command:&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;bit add &amp;lt;directory-path&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;Smooth integration of Bit into existing project structures.&lt;/li&gt;
&lt;li&gt;Allows incremental adoption without disrupting the current workflow.&lt;/li&gt;
&lt;li&gt;Maintains the modularity and independence of components within a larger project.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--y16L_fBU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vnvkphlb93quwr2dr5lb.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--y16L_fBU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vnvkphlb93quwr2dr5lb.jpg" alt="Shifting Monolith to Composable software" width="800" height="529"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;Identify a folder in legacy code to convert into a Bit component.&lt;/li&gt;
&lt;li&gt;Ensure there are no relative imports from the same legacy project. If present, convert those to Bit components too.&lt;/li&gt;
&lt;li&gt;Use bit add to track the folder as a Bit component:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bit add &amp;lt;path-to-a-scary-legacy-folder&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Creating a Component by Forking an Existing Bit Component
&lt;/h2&gt;

&lt;p&gt;Bit Fork is used when you want to use an existing Bit component as the starting point for your own (new) Bit component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Command:&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;bit fork &amp;lt;component-id&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;Enables customization of components for specific project needs (when extending the component's API is not enough)&lt;/li&gt;
&lt;li&gt;Applicable in scenarios with limited permissions to a scope. i.e, when you have read permissions but no write permissions. - You can clone a component but not suggest changes to the origin component.&lt;/li&gt;
&lt;li&gt;Ensures the independence of the forked component, facilitating innovation and adaptation.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ol&gt;
&lt;li&gt;Identify a component dependency that needs modification but is in a scope where you lack permissions.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hACHGsoV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lyk39kvwk8cknx1aa9tl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hACHGsoV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lyk39kvwk8cknx1aa9tl.png" alt="Image description" width="800" height="509"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flexibility and Control with Bit Fork&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When working with components in Bit, you might encounter situations where you need to modify a component but don't have the necessary permissions. Bit offers two approaches for this: using lanes to suggest changes and forking for creating a separate instance.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Using Lanes for Change Requests&lt;/strong&gt;: If you need to suggest changes to a component, such as the &lt;a href="https://bit.cloud/teambit/design/responsive/breakpoints"&gt;breakpoints component&lt;/a&gt; in the &lt;a href="https://bit.cloud/teambit/design/"&gt;design scope&lt;/a&gt;, but don't have direct modification permissions, you can use lanes. Lanes in Bit allow you to propose modifications or improvements, which can then be reviewed and potentially merged by those with the appropriate permissions. For more information on using lanes in Bit, check out the &lt;a href="https://bit.dev/reference/change-requests/building-lanes"&gt;Bit Lanes Documentation&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Forking for Independent Instances&lt;/strong&gt;: If creating a separate instance of a component works for your scenario, such as needing a version of the &lt;a href="https://bit.cloud/teambit/community/introduction/meet-bit-component"&gt;meet-bit-component&lt;/a&gt; with specific alterations, you can use the Bit Fork command. This is particularly useful if you don't need to change the source of truth but require a version tailored to your project's needs.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Forking in Bit is different from Git forking. In Bit, forking a component results in a completely independent instance from the original, not linked for future merges, which is crucial for component-based development that requires customization and flexibility.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;2.Fork the dependency to your scope using bit fork, allowing for necessary modifications:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bit fork teambit.design/responsive/breakpoints
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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

&lt;p&gt;Through Bit Create, Bit Add, and Bit Fork, Bit provides a comprehensive approach to managing component-based systems in software development. These tools promote efficient, modular, and collaborative development, streamlining the process of creating, integrating, and customizing components in various project environments.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>bitdev</category>
      <category>react</category>
      <category>component</category>
    </item>
    <item>
      <title>Using Refs and Hooks to call child function in parent component</title>
      <dc:creator>Nitsan Cohen</dc:creator>
      <pubDate>Tue, 17 Jan 2023 19:42:54 +0000</pubDate>
      <link>https://forem.com/nitsancohen770/using-refs-and-hooks-to-call-child-function-in-parent-component-4mm3</link>
      <guid>https://forem.com/nitsancohen770/using-refs-and-hooks-to-call-child-function-in-parent-component-4mm3</guid>
      <description>&lt;p&gt;In React, it's generally best practice to pass down functionality to children through props and to pass up notifications from children through events. However, there may be times when you need to expose an imperative method on a child component to be called from the parent component. In these cases, you can use refs.&lt;/p&gt;

&lt;p&gt;To use refs in functional components, you'll need to use the forwardRef, useRef, and useImperativeHandle hooks from the React library.&lt;/p&gt;

&lt;p&gt;First, you'll need to wrap your child component in the forwardRef function in order to access the ref object that is assigned using the ref prop. The ref is passed as the second parameter to the function component.&lt;/p&gt;

&lt;p&gt;Next, you'll need to use the useImperativeHandle hook to extend the component instance with the imperative method you want to expose.&lt;/p&gt;

&lt;p&gt;In the parent component, you'll need to use the useRef hook to get a reference to the child component, and then use this reference to call the child's method.&lt;/p&gt;

&lt;p&gt;Here's an example of how to use refs to call a child method from a parent component in modern React:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { forwardRef, useRef, useImperativeHandle } = React;

const Child = forwardRef((props, ref) =&amp;gt; {
  const childRef = useRef();
  useImperativeHandle(ref, () =&amp;gt; ({
    getAlert() {
      alert("You clicked the button!");
    }
  }));

  return &amp;lt;h1&amp;gt;Child&amp;lt;/h1&amp;gt;;
});

const Parent = () =&amp;gt; {
  const childRef = useRef();
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;Child ref={childRef} /&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; childRef.current.getAlert()}&amp;gt;Click&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

ReactDOM.render(
  &amp;lt;Parent /&amp;gt;,
  document.getElementById('root')
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please keep in mind that using refs in this way is an escape hatch, and usually indicates that a better design is available.&lt;/p&gt;

</description>
      <category>nocode</category>
      <category>webdev</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Extendible React Components</title>
      <dc:creator>Nitsan Cohen</dc:creator>
      <pubDate>Wed, 26 Jan 2022 13:40:11 +0000</pubDate>
      <link>https://forem.com/nitsancohen770/extendible-react-components-1a81</link>
      <guid>https://forem.com/nitsancohen770/extendible-react-components-1a81</guid>
      <description>&lt;p&gt;Here's a nice React tip for you.&lt;/p&gt;

&lt;p&gt;When creating components using TypeScript (please use TS), always extend the props type with a &lt;br&gt;
React.HTMLAttributes&lt;br&gt;
(you can replace the DivElement with any other element you are returning from your JSX).&lt;/p&gt;

&lt;p&gt;That will allow you to have all the properties for that component correctly typed, and you can pass them to the JSX element you are returning.&lt;/p&gt;

&lt;p&gt;A handy use case is where we want to create a component with specific styles but make it extendible so that other consumers that install that component in their app will be able to override the styles we defined with their own styles.&lt;/p&gt;

&lt;p&gt;According to the CSS specificity rules, for their styles to apply, you have to pass their className as the last one in order (since they are both using the class selector, the latest styles that appear are those who apply).&lt;/p&gt;

&lt;p&gt;In the following snippet, you'll notice how we pass the className attribute we destruct from props and use the classNames library to join them nicely:&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, { ReactNode } from "react";
import { Link } from "@teambit/base-react.navigation.link";
import { Arrow } from "./arrow";
import styles from "./styles.module.scss";
import classNames from "classnames";

export type BackButtonProps = {} &amp;amp; React.HtmlHTMLAttributes&amp;lt;HTMLDivElement&amp;gt;;

export function BackButton({ className, ...rest }: BackButtonProps) {
  return (
    &amp;lt;div
      {...rest}
      className={classNames(styles.backButtonContainer, className)}
    &amp;gt;
      &amp;lt;Link href="/blog"&amp;gt;
        &amp;lt;Arrow /&amp;gt;
        &amp;lt;span className={styles.backButtonText}&amp;gt;Back to Blog&amp;lt;/span&amp;gt;
      &amp;lt;/Link&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we said, it will allow users to pass their own styles, and if there are any conflicting styles, the consumers' classes will override the default styles.&lt;/p&gt;

&lt;p&gt;Always think about who is going to consume your component. You will probably be the first one consuming it, but you never know who else will come after you and install your component—extendible Components FTW.&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;&lt;p&gt;For more posts like this &lt;a href="https://www.linkedin.com/in/nitsan-cohen/"&gt;follow me on LinkedIn&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I work as frontend &amp;amp; content developer for &lt;a href="//bit.dev"&gt;Bit&lt;/a&gt; - a toolchain for component-driven development (Forget monolithic apps and distribute to component-driven software).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>onClick handlers with Currying (React)</title>
      <dc:creator>Nitsan Cohen</dc:creator>
      <pubDate>Thu, 20 Jan 2022 10:35:02 +0000</pubDate>
      <link>https://forem.com/nitsancohen770/onclick-handlers-with-currying-react-4aj4</link>
      <guid>https://forem.com/nitsancohen770/onclick-handlers-with-currying-react-4aj4</guid>
      <description>&lt;p&gt;How do you pass a function with an argument to an onClick event in React?&lt;/p&gt;

&lt;p&gt;You are probably all familiar with the fact that when passing a function that takes an argument to an onClick event (or any other event), you have to wrap it with an anonymous callback function.&lt;/p&gt;

&lt;p&gt;For instance:&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;button onClick = { () =&amp;gt; myFunction(argument) } /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We do it because we want to have a pointer to a function triggered upon a click. If we pass the function without a callback, it will get fired right when the DOM is mounted.&lt;/p&gt;

&lt;p&gt;But there is another option. We can pass an anonymous callback to our handler. This way, our handler itself will serve as a pointer, and we will not have to give an anonymous function to our onClick event. The name of this design pattern is called currying.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myHandler = (text:string) =&amp;gt; () =&amp;gt; alert(`${text}`)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When is this useful?&lt;/p&gt;

&lt;p&gt;It could be helpful when we intend to use our handler in many events (for example, mapping an array). Instead of creating useless callback functions for each event, we create them only once in the handler.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fu5y40958np92nccpxubx.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fu5y40958np92nccpxubx.jpeg" alt="currying react example"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Two ways to auto focus React input element</title>
      <dc:creator>Nitsan Cohen</dc:creator>
      <pubDate>Thu, 13 Jan 2022 10:00:57 +0000</pubDate>
      <link>https://forem.com/nitsancohen770/two-ways-to-auto-focus-react-input-element-3pcf</link>
      <guid>https://forem.com/nitsancohen770/two-ways-to-auto-focus-react-input-element-3pcf</guid>
      <description>&lt;p&gt;If you want to focus an input element when the DOM mounts, you have to place a ref on your input element and trigger the focus function in the useEffect hook.&lt;/p&gt;

&lt;p&gt;Calling that function in the useEffect hook is because the callback that we pass to this hook will be triggered only when the DOM finishes to mount. Otherwise, we risk triggering the focus function before the input element is on the screen.&lt;/p&gt;

&lt;p&gt;But there is a more straightforward option. You can pass the autoFocus attribute to that input element.&lt;/p&gt;

&lt;p&gt;Nevertheless, it is sometimes helpful to use the first option to focus on an input element. For instance, we want our user to focus on the last input element when refreshing the page or return to that page from another one. &lt;/p&gt;

&lt;p&gt;The attached example stores the latest input value in the local storage. We have to use the state as well to cause a new render cycle and keep the newest value. We are also checking if there is a value before setting it to local storage. This is meant to prevent empty strings from being stored.&lt;/p&gt;

&lt;p&gt;Live playground:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackblitz.com/edit/react-8arjym?file=src/App.js" rel="noopener noreferrer"&gt;https://stackblitz.com/edit/react-8arjym?file=src/App.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fh2cvt68mfsxiedetz0nk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fh2cvt68mfsxiedetz0nk.png" alt="auto focus react"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;&lt;p&gt;For more posts like this &lt;a href="https://www.linkedin.com/in/nitsan-cohen/" rel="noopener noreferrer"&gt;follow me on LinkedIn&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I work as frontend &amp;amp; content developer for &lt;a href="//bit.dev"&gt;Bit&lt;/a&gt; - a toolchain for component-driven development (Forget monolithic apps and distribute to component-driven software).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Components search by Bit</title>
      <dc:creator>Nitsan Cohen</dc:creator>
      <pubDate>Wed, 12 Jan 2022 20:36:13 +0000</pubDate>
      <link>https://forem.com/nitsancohen770/components-search-by-bit-5aa3</link>
      <guid>https://forem.com/nitsancohen770/components-search-by-bit-5aa3</guid>
      <description>&lt;p&gt;Components search by Bit is extremely powerful. Being able to search across all your components, narrow it down to just one org but still search across all scopes in that org, find the component you are looking for and then install it in your project&lt;/p&gt;

&lt;p&gt;Mind-blowing and it is so fast. Really love what Bit is doing. So many cool things. Make sure you sign up to get access.&lt;/p&gt;

&lt;p&gt;Check out the video I made here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/posts/nitsan-cohen_webdev-components-javascript-activity-6887018727347367937-PGI3"&gt;https://www.linkedin.com/posts/nitsan-cohen_webdev-components-javascript-activity-6887018727347367937-PGI3&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>react</category>
    </item>
    <item>
      <title>utilize the power of 
useState and useCallback hooks in React</title>
      <dc:creator>Nitsan Cohen</dc:creator>
      <pubDate>Wed, 12 Jan 2022 07:46:45 +0000</pubDate>
      <link>https://forem.com/nitsancohen770/utilize-the-power-of-usestate-and-usecallback-hooks-in-react-4d1e</link>
      <guid>https://forem.com/nitsancohen770/utilize-the-power-of-usestate-and-usecallback-hooks-in-react-4d1e</guid>
      <description>&lt;p&gt;What is the right way of using the useCallback and useState hooks in conjunction?&lt;/p&gt;

&lt;p&gt;Sometimes we will want to wrap the useState hook with a useCallback hook.&lt;/p&gt;

&lt;p&gt;The simple reason for that is to prevent a render cycle when passing the function as props (when the component receiving that prop is wrapped with &lt;code&gt;React.memo&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;We must remember is that the &lt;code&gt;setState&lt;/code&gt; function must not be passed to the &lt;code&gt;useCallback&lt;/code&gt; dependency array. The React team suggests this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"React guarantees that setState function identity is stable and won’t change on re-renders. This is why it’s safe to omit from the useEffect or useCallback dependency list."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For example:&lt;/p&gt;

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

const [isOpen, setIsOpen] = useState(false); 

const toggle = useCallback(() =&amp;gt; setIsOpen(!isOpen), [isOpen]); // We don't have to pass the setState function.


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

&lt;/div&gt;

&lt;p&gt;But the example above is but practice. Why? Even though we are not passing the setState function, we must pass the &lt;code&gt;isOpen&lt;/code&gt; variable, as it is being used inside the &lt;code&gt;useCallback&lt;/code&gt; hook. This will cause the toggle function to be recreated every time the state changes.&lt;/p&gt;

&lt;p&gt;You already know the solution, right?&lt;/p&gt;

&lt;p&gt;We can use the callback function that receives the previous value and manipulate it. This way, we are not using variables from outside the useCallback function, and we can keep the dependency array nice and clean.&lt;/p&gt;

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

const [isOpen, setIsOpen] = useState(false); 

const toggle = useCallback(() =&amp;gt; setIsOpen(prevState =&amp;gt;!prevState), []);



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

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.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%2F6yy1oogofa3ll1lncuav.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F6yy1oogofa3ll1lncuav.png" alt="Code snippet"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;&lt;p&gt;For more posts like this &lt;a href="https://www.linkedin.com/in/nitsan-cohen/" rel="noopener noreferrer"&gt;follow me on LinkedIn&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I work as frontend &amp;amp; content developer for &lt;a href="//bit.dev"&gt;Bit&lt;/a&gt; - a toolchain for component-driven development (Forget monolithic apps and distribute to component-driven software).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>What is Webpack? In simple words.</title>
      <dc:creator>Nitsan Cohen</dc:creator>
      <pubDate>Thu, 06 Jan 2022 07:27:31 +0000</pubDate>
      <link>https://forem.com/nitsancohen770/what-is-webpack-in-simple-words-31fe</link>
      <guid>https://forem.com/nitsancohen770/what-is-webpack-in-simple-words-31fe</guid>
      <description>&lt;p&gt;What is Webpack?&lt;/p&gt;

&lt;p&gt;Once upon a time, there was only &lt;code&gt;HTML&lt;/code&gt;. &lt;code&gt;HTML&lt;/code&gt; was the only building block of the whole web.&lt;/p&gt;

&lt;p&gt;Then came JavaScript but its usage was very basic and minimal. You could see websites with a "HUGE" JavaScript bundle of:&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;script&amp;gt;
alert("Welcome to my website")
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Today, not only JavaScript is used everywhere, but there are some frameworks (like React), where the user does not get from the server any HTML code (only an empty file)! He gets a bundle of JavaScript which transpile to HTML (and sometimes also CSS) by the users' browser.&lt;/p&gt;

&lt;p&gt;Anyhow, we have to deal and manage with a lot of JavaScript code these days and this is where Webpack comes into the picture.&lt;/p&gt;

&lt;p&gt;When dealing with a lot of code, we want to split our code into small pieces. It makes our code more readable and maintainable. You are probably already familiar with the import-export syntax. This is code-splitting.&lt;/p&gt;

&lt;p&gt;But eventually, we will want to transform all of our files into (usually) one big bundle. This bundle should be optimized and should work (polyfill) on all the browsers we expect our code will run at.&lt;/p&gt;

&lt;p&gt;Webpack does exactly that. It will run on build time and create an optimized bundle of your code (-small-sized bundle). For example, it can change the name of your variables from:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const example = 10;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var e = 10;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is optimized because it takes less memory (fewer letters to store) and runs faster on the browser (the JavaScript engine runs it faster). &lt;/p&gt;

&lt;p&gt;Another aspect, as mentioned, is Polyfilling. We can configure our Webpack setup to make our code run on the browsers we define for it. This is actually done with the help of another library (usually Babel).&lt;/p&gt;

&lt;p&gt;In the example above, Webpack is changing the const keyword to var, to make it work on more browsers.&lt;/p&gt;

&lt;p&gt;Let's go through a basic Webpack configuration for a React app (open the attached picture side by side).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Feo4xb1ckx4nsyb4j765q.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Feo4xb1ckx4nsyb4j765q.jpeg" alt="what-is-webpack"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Entry&lt;/strong&gt; - the entry destination is the main file we want Webpack to start its bundling. We only have to specify one file and Webpack will figure out the rest by looking for its dependencies. Here we specify the index.js file which is the place where we render our App in React (and all other files are just siblings of it).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt; - is the path where we want our bundle to be. We specify directory name (here it's dist) and we can also specify the file name we want to output.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Loaders&lt;/strong&gt; - Webpack is not very smart. It understands only JS and JSON files. If we want Wepback to understand JSX, for example, we have to specify loaders that will translate it for Webpack. Here we specify the babel-loader and set its options to work with JSX (React).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Plugins&lt;/strong&gt; - These help us to make our code more optimized and handle assets. In our example, we use the HtmlWebpackPlugin. This plugin will generate an HTML file and will automatically inject our JS bundle into it.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Don't be afraid of your Webpack. It's a great tool and you should be comfortable enough with it so you can make config changes to it.&lt;br&gt;
I encourage you to go and play with Webpack.&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;&lt;p&gt;For more posts like this &lt;a href="https://www.linkedin.com/in/nitsan-cohen/" rel="noopener noreferrer"&gt;follow me on LinkedIn&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I work as frontend &amp;amp; content developer for &lt;a href="//bit.dev"&gt;Bit&lt;/a&gt; - a toolchain for component-driven development (Forget monolithic apps and distribute to component-driven software).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Cascading what? Explanation about CSS Cascading rules</title>
      <dc:creator>Nitsan Cohen</dc:creator>
      <pubDate>Wed, 05 Jan 2022 06:40:28 +0000</pubDate>
      <link>https://forem.com/nitsancohen770/cascading-what-explanation-about-css-cascading-rules-565j</link>
      <guid>https://forem.com/nitsancohen770/cascading-what-explanation-about-css-cascading-rules-565j</guid>
      <description>&lt;p&gt;CSS stands for Cascading Style Sheets&lt;/p&gt;

&lt;p&gt;I want to talk with you about the cascading part today.&lt;/p&gt;

&lt;p&gt;You probably already know that CSS is not a programming language. Instead, CSS is all about declaring rules to the elements in our DOM.&lt;/p&gt;

&lt;p&gt;We have all kinds of ways to declare those rules.&lt;br&gt;
For example, inline styles:&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 style="backgroundColor:red" class="my_div"&amp;gt;Hi &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or class selectors:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.my_div {
background-color:green;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But how do these rules apply? Which one is more powerful?&lt;/p&gt;

&lt;p&gt;That is where the cascading rules come into action. The inline style will rule out the class selector in the example above.&lt;/p&gt;

&lt;p&gt;I sometimes see people struggling with the cascading rules, but actually, it is straightforward.&lt;/p&gt;

&lt;p&gt;When conflicting styles appear, the first thing your browser will check is the origin of the stylesheet.&lt;/p&gt;

&lt;p&gt;We have two main types of origins:&lt;/p&gt;

&lt;p&gt;-user-agent styles, which in simple words means &lt;strong&gt;browsers' default&lt;/strong&gt;.&lt;br&gt;
-Author styles, which in simple terms means - &lt;strong&gt;your custom styles&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The author styles will always override the user-agent styles.&lt;/p&gt;

&lt;p&gt;So far, so good. But what if we have conflicts in the author styles themselves?&lt;/p&gt;

&lt;p&gt;Well, it's effortless to remember that the winner is always inline styles.&lt;/p&gt;

&lt;p&gt;After that, we get to the scary (and problematic to pronounce) word specificity.&lt;/p&gt;

&lt;p&gt;Just memorize it: IDs, Classes, attributes, pseudo-classes, elements, pseudo-elements.&lt;/p&gt;

&lt;p&gt;Or in symbols:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#id (avoid using them!)
.class (Classes).
h1[lang="pt"] (attributes).
h1: hover (pseudo-classes).
h1 (elements).
h1:: first-letter (pseudo-elements).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lastly, if no specificity rules apply, the browser will use the rule that appears later in the source.&lt;/p&gt;

&lt;p&gt;I've created a nice diagram to help you with that. Please &lt;strong&gt;learn it by heart&lt;/strong&gt; if you dare call yourself a frontend developer :)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fcgw6ofnjlw208y6uszk9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fcgw6ofnjlw208y6uszk9.png" alt="css cascading rules"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;0h, and don't use !important (unless you have a significant reason).&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;&lt;p&gt;For more posts like this &lt;a href="https://www.linkedin.com/in/nitsan-cohen/" rel="noopener noreferrer"&gt;follow me on LinkedIn&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I work as frontend &amp;amp; content developer for &lt;a href="//bit.dev"&gt;Bit&lt;/a&gt; - a toolchain for component-driven development (Forget monolithic apps and distribute to component-driven software).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>css</category>
      <category>beginners</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Code splitting (bundle-split) in React</title>
      <dc:creator>Nitsan Cohen</dc:creator>
      <pubDate>Tue, 04 Jan 2022 07:56:04 +0000</pubDate>
      <link>https://forem.com/nitsancohen770/code-splitting-bundle-split-in-react-1ffh</link>
      <guid>https://forem.com/nitsancohen770/code-splitting-bundle-split-in-react-1ffh</guid>
      <description>&lt;p&gt;As you probably already know, when you build your React app (npm run build), Webpack will bundle all of your files into one big chunk.&lt;/p&gt;

&lt;p&gt;If your app is not that big that's usually just fine. But what happens when your app grows dramatically? You can end up with a really big JS file and that can hurt your UX, especially the initial loading time.&lt;/p&gt;

&lt;p&gt;Luckily there is a simple solution we can implement. Its name is code-splitting. By splitting our bundle, we can tell React (or Webpack) to load only the code that is absolutely necessary for the initial load (-for the current page, for example).&lt;/p&gt;

&lt;p&gt;Later, we can "lazy load" the rest of our code upon request (when navigating or scrolling down).&lt;/p&gt;

&lt;p&gt;One of the easiest ways to implement code splitting is by using a dynamic import. As the name suggests, the dynamic import will asynchronously import our file and will return a promise.&lt;/p&gt;

&lt;p&gt;It looks 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;export default (numA, numB) =&amp;gt; numA * numB

async function loadModule() {

 const dynamicImport = await import('./file1')

 dynamicImport.default(10, 10)
}

loadModule() //output: 100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But what if we want to dynamically import a component?&lt;br&gt;
Let's assume we have a component that is used by our app only on page B. We don't want to import it when we are on page A. Well, with &lt;code&gt;React.lazy&lt;/code&gt; it's a breeze!&lt;/p&gt;

&lt;p&gt;Have a look at the attached image. All we have to do is use React.lazy to import our component. This component will now be excluded from our bundle and imported only on runtime when we load that specific page.&lt;br&gt;
We are also using the Suspense component to render a fallback while the user is retrieving the bundle.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fxfzjnk9dr7ggaptbt8rs.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fxfzjnk9dr7ggaptbt8rs.jpeg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;&lt;p&gt;For more posts like this &lt;a href="https://www.linkedin.com/in/nitsan-cohen/" rel="noopener noreferrer"&gt;follow me on LinkedIn&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I work as frontend &amp;amp; content developer for &lt;a href="//bit.dev"&gt;Bit&lt;/a&gt; - a toolchain for component-driven development (Forget monolithic apps and distribute to component-driven software).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>beginners</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Use TypeScript Generics to enhance your React components and make them reusable</title>
      <dc:creator>Nitsan Cohen</dc:creator>
      <pubDate>Mon, 03 Jan 2022 06:50:01 +0000</pubDate>
      <link>https://forem.com/nitsancohen770/use-typescript-generics-to-enhance-your-react-components-and-make-them-reusable-431f</link>
      <guid>https://forem.com/nitsancohen770/use-typescript-generics-to-enhance-your-react-components-and-make-them-reusable-431f</guid>
      <description>&lt;p&gt;Use TypeScript Generics to enhance your React components and make them reusable!&lt;/p&gt;

&lt;p&gt;What is TypeScript Generics?&lt;/p&gt;

&lt;p&gt;Generics allow us to have a dynamic type for our components. It gives us more flexibility when building components and allows better reusability for the consumers of the components.&lt;/p&gt;

&lt;p&gt;Let's have a look at a simple example of Generics:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function returnWhatItGets&amp;lt;Type&amp;gt;(arg: Type): Type =&amp;gt; arg;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The part where we write &lt;code&gt;&amp;lt; Type&amp;gt;&lt;/code&gt; tells typescript that when we use this function, we will be able to insert a dynamic type that will serve as the Type of our argument (&lt;code&gt;arg&lt;/code&gt;) and the Type of the return value of this function.&lt;/p&gt;

&lt;p&gt;That is how it looks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numberOutput = returnWhatItGets&amp;lt;number&amp;gt;(5);

const stringOutput = returnWhatItGets&amp;lt;string&amp;gt;("hi");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we're all on the same page with Generics, let's implement generics in our React component!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 import React from 'react';
2
3 export type GenericComponentProps&amp;lt;Type&amp;gt; = {
4 data: Type[];
5 };
6
7 export function GenericComponent&amp;lt;Type&amp;gt;({ data }: 8GenericComponentProps&amp;lt;Type&amp;gt;) {
9 return (
10 &amp;lt;&amp;gt;
11 {data?.map((item: Type, index: number) =&amp;gt; (
12 &amp;lt;ul key={index}&amp;gt;
13 {(Object.keys(data[0]) as Array&amp;lt;keyof Type&amp;gt;).map((key) =&amp;gt; (
14 &amp;lt;li key={key.toString()}&amp;gt;{item[key]}&amp;lt;/li&amp;gt;
15 ))}
16 &amp;lt;/ul&amp;gt;
17 ))}
&amp;lt;/&amp;gt;
);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It might not look very clear at first glance, but it's really straightforward. Let's go through the code line by line:&lt;/p&gt;

&lt;p&gt;On line number 3, we define a Generic type for our component's props (Type).&lt;/p&gt;

&lt;p&gt;In line 4, we assume that our props object will have the "data" property. Our data type will be an array of the dynamic Type we will initiate our component.&lt;/p&gt;

&lt;p&gt;Moving forward to line 7, we define our functional component with the generic Type that will be passed down to the props object's Type.&lt;/p&gt;

&lt;p&gt;Our data is an array with our custom Type, so in line 11, we begin mapping that array. For each object, we output an&lt;code&gt;&amp;lt;ul&amp;gt;&lt;/code&gt;  attribute.&lt;/p&gt;

&lt;p&gt;Now we want to output all the properties values of the given object, so we turn our object to an array of its keys using the Object.keys method and map it to print the value for each property. Notice how we define the type for this array dynamically with the keyof syntax.&lt;/p&gt;

&lt;p&gt;Now let's test our 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;GenericComponent
   data={[
    { name: 'Nitsan', city: 'Harish' },
    { name: 'Abraham', city: 'Beer Sheva' },
   ]}
  /&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;That's it! We get all the benefits of typescript, such as auto-completion and type checking, while creating flexible and reusable components.&lt;/p&gt;

&lt;p&gt;Using tools to keep track and build independent components, such as &lt;a href="//bit.dev"&gt;Bit&lt;/a&gt;, our users benefit significantly from such a flexible setup.&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;&lt;p&gt;For more posts like this &lt;a href="https://www.linkedin.com/in/nitsan-cohen/"&gt;follow me on LinkedIn&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I work as frontend &amp;amp; content developer for &lt;a href="//bit.dev"&gt;Bit&lt;/a&gt; - a toolchain for component-driven development (Forget monolithic apps and distribute to component-driven software).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>webdev</category>
      <category>react</category>
    </item>
    <item>
      <title>pnpm - The Best Package Manager</title>
      <dc:creator>Nitsan Cohen</dc:creator>
      <pubDate>Sun, 02 Jan 2022 09:24:19 +0000</pubDate>
      <link>https://forem.com/nitsancohen770/pnpm-the-best-package-manager-57fj</link>
      <guid>https://forem.com/nitsancohen770/pnpm-the-best-package-manager-57fj</guid>
      <description>&lt;p&gt;What package manager do you use in your projects?&lt;/p&gt;

&lt;p&gt;npm? yarn?&lt;/p&gt;

&lt;p&gt;If none of the above is true, you must be using pnpm, and let me say - that's a great choice.&lt;/p&gt;

&lt;p&gt;Haven't you heard about pnpm? Let's have a short review.&lt;/p&gt;

&lt;p&gt;pnpm is fast. How fast? In most cases, it’s quicker than npm and yarn. It can even get to three times faster than npm on a fresh install!&lt;/p&gt;

&lt;p&gt;But not only that. pnpm will save you LOTS of gigabytes on your computer.&lt;/p&gt;

&lt;p&gt;The magic behind pnpm is with how it handles your packages.&lt;/p&gt;

&lt;p&gt;Let's say you have installed the famous  node.js framework express using pnpm.&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 -g pnpm  // If you’re using Node.js version 16.13 or higher, you get pnpm out-of-the-box. All you have to do is enable it using “corepack enable” command.
&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;pnpm init &amp;amp;&amp;amp; pnpm i express 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Express has a bunch of dependencies itself, as you can examine in its package.json (array-flatten, body-parser, and many more).&lt;/p&gt;

&lt;p&gt;Where do we store the dependencies of Express (sub-dependencies)?&lt;/p&gt;

&lt;p&gt;npm will have to create another node_modules folder in the express folder, but this is not the case with pnpm.&lt;/p&gt;

&lt;p&gt;Actually, this is not the case with npm either.  Historically (from npm version 2 and below), npm was using nested node_modules, but due to a limitation on windows (windows allows only up to 256 chars for directories paths) and some more reasons, they changed to the way it works.&lt;/p&gt;

&lt;p&gt;They now use an algorithm to hoist all sub-dependencies to the root directory of the node_modules.&lt;/p&gt;

&lt;p&gt;But this method has a disadvantage:&lt;/p&gt;

&lt;p&gt;There could be a situation where I use one of the sub dependencies in my project, but I didn’t install them. &lt;/p&gt;

&lt;p&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;const parser = require('body-parser');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I didn't install this package (it's not in my package.json dependencies, only in Express package.json), but I can use it since npm hoisted it to my root node_modules.&lt;/p&gt;

&lt;p&gt;There's no problem as long as the maintainers of Express decide that body-parser stays part of its dependencies. &lt;/p&gt;

&lt;p&gt;However, if they decide to remove it on a later version, this will break my project (I'm trying to import from a package that does not exist).&lt;/p&gt;

&lt;p&gt;pnpm approach is more intelligent. It stores all the packages in a folder called &lt;code&gt;.pnpm&lt;/code&gt; in your node_modules folder. It will then use a symbolic link (symlink) to point to that folder. &lt;/p&gt;

&lt;p&gt;This way, we avoid deeply nested folders while preventing the possibility of importing packages that we didn't install in our project.&lt;/p&gt;

&lt;p&gt;Lastly and most importantly, as we mentioned, pnpm will save you tons of storage in your computer.&lt;/p&gt;

&lt;p&gt;How? pretty simple. Let's continue with the Express example. When installing Express in our project, pnpm will first save all of its files in a global store (using hashes). This way, the next time we install Express (in another project), pnpm will hard link to the files that exist in the global store.&lt;/p&gt;

&lt;p&gt;That's also the reason that in &lt;a href="//bit.dev"&gt;Bit&lt;/a&gt; we use pnpm as the default package manager to manage your components dependencies. &lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;&lt;p&gt;For more posts like this &lt;a href="https://www.linkedin.com/in/nitsan-cohen/"&gt;follow me on LinkedIn&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I work as frontend &amp;amp; content developer for &lt;a href="//bit.dev"&gt;Bit&lt;/a&gt; - a toolchain for component-driven development (Forget monolithic apps and distribute to component-driven software).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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