AI-Powered Web Development: Create Stunning Sites Faster – Beginners Guide
Building a website using artificial intelligence (AI) can streamline the development process and significantly cut costs ...
Building a website using artificial intelligence (AI) can streamline the development process and significantly cut costs ...
<div><h3> <strong>Introduction</strong> </h3> <p>In the ever-evolving world of software development, maintaining code quality and consistency is crucial for successful team collaboration. As projects grow in complexity and involve multiple developers, the risk of introducing bugs, inconsistent coding styles, and maintainability issues increases. Fortunately, modern tools and practices have emerged to address these challenges, enabling teams to work more efficiently and deliver high-quality code.</p> <p>In this blog post, we'll explore the power of tools like Husky, ESLint, Prettier, and more, and how they can enhance your team's development workflow.</p> <h3> <strong>The Importance of Code Quality and Consistency</strong> </h3> <p>Code quality and consistency are essential for any software project, but they become even more critical when working in a team environment. Poorly written or inconsistent code can lead to increased technical debt, making it harder for developers to understand, maintain, and extend the codebase over time. This, in turn, can significantly impact team productivity and project timelines.</p> <p>Overlooked errors and style inconsistencies can also introduce bugs and make it challenging to collaborate effectively. Imagine a situation where one developer follows a specific coding style, while another has a different approach. This can make code reviews more time-consuming and increase the risk of merge conflicts, ultimately slowing down the development process.</p> <p>Fortunately, automated tools can help teams enforce coding standards, identify potential issues, and maintain a consistent codebase across the entire project.</p> <h3> <strong>Introducing Husky: The Pre-Commit Hook Powerhouse</strong> </h3> <p>Husky is a powerful tool that allows you to run scripts before committing code or pushing changes to a remote repository. It's particularly useful for enforcing code quality checks and preventing developers from accidentally committing code that doesn't adhere to the team's coding standards.</p> <p>With Husky, you can define pre-commit hooks that run specific scripts before each commit. For example, you could set up a pre-commit hook that runs ESLint (a code linting tool) and Prettier (a code formatting tool) to ensure that your code is free of errors and follows consistent styling conventions.</p> <ul> <li> <strong>Setting up Husky in Your Project</strong> </li> </ul> <div class="highlight js-code-highlight"> <pre class="highlight javascript"><code><span class="err">#</span> <span class="nx">Using</span> <span class="nx">npm</span> <span class="nx">npm</span> <span class="nx">install</span> <span class="nx">husky</span> <span class="o">--</span><span class="nx">save</span><span class="o">-</span><span class="nx">dev</span> <span class="err">#</span> <span class="nx">Using</span> <span class="nx">yarn</span> <span class="nx">yarn</span> <span class="nx">add</span> <span class="nx">husky</span> <span class="o">--</span><span class="nx">dev</span> </code></pre> </div> <p>By using pre-commit hooks with Husky, you can catch potential issues early in the development process, reducing the risk of introducing bugs and maintaining a clean, consistent codebase.</p> <h3> <strong>Enforcing Code Quality with ESLint: The JavaScript Linting Powerhouse</strong> </h3> <p>ESLint is a powerful tool for identifying and fixing problems in JavaScript code. It can help teams enforce coding standards, detect potential bugs, and promote best practices, ensuring that the codebase remains maintainable and easy to understand.</p> <ul> <li> <strong>Setting up ESLint in Your Project</strong> </li> </ul> <div class="highlight js-code-highlight"> <pre class="highlight javascript"><code><span class="err">#</span> <span class="nx">Using</span> <span class="nx">npm</span> <span class="nx">npm</span> <span class="nx">install</span> <span class="nx">eslint</span> <span class="o">--</span><span class="nx">save</span><span class="o">-</span><span class="nx">dev</span> <span class="err">#</span> <span class="nx">Using</span> <span class="nx">yarn</span> <span class="nx">yarn</span> <span class="nx">add</span> <span class="nx">eslint</span> <span class="o">--</span><span class="nx">dev</span> </code></pre> </div> <p>ESLint provides a wide range of rules that can help you catch potential issues, such as unused variables, missing semicolons, inconsistent naming conventions, and more. By enforcing these rules across your codebase, you can maintain a consistent coding style and prevent common coding mistakes, ultimately improving code quality and maintainability.</p> <h3> <strong>Ensuring Code Consistency with Prettier: The Code Formatting Guru</strong> </h3> <p>While ESLint focuses on code linting and identifying potential issues, Prettier is a tool designed specifically for code formatting. It helps teams maintain a consistent coding style by automatically formatting code according to predefined rules.</p> <ul> <li> <strong>Integrating Prettier into Your Workflow</strong> </li> </ul> <div class="highlight js-code-highlight"> <pre class="highlight javascript"><code><span class="err">#</span> <span class="nx">Using</span> <span class="nx">npm</span> <span class="nx">npm</span> <span class="nx">install</span> <span class="nx">prettier</span><span class="o">--</span><span class="nx">save</span><span class="o">-</span><span class="nx">dev</span> <span class="err">#</span> <span class="nx">Using</span> <span class="nx">yarn</span> <span class="nx">yarn</span> <span class="nx">add</span> <span class="nx">prettier</span> <span class="o">--</span><span class="nx">dev</span> </code></pre> </div> <div class="highlight js-code-highlight"> <pre class="highlight javascript"><code><span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="p">{</span> <span class="na">semi</span><span class="p">:</span> <span class="kc">true</span><span class="p">,</span> <span class="na">singleQuote</span><span class="p">:</span> <span class="kc">true</span><span class="p">,</span> <span class="na">trailingComma</span><span class="p">:</span> <span class="dl">'</span><span class="s1">all</span><span class="dl">'</span><span class="p">,</span> <span class="na">printWidth</span><span class="p">:</span> <span class="mi">80</span><span class="p">,</span> <span class="c1">// Add more rules as needed</span> <span class="p">};</span> </code></pre> </div> <p>By enforcing consistent code formatting across your codebase, Prettier reduces the cognitive load for developers when reading and reviewing code. It also helps eliminate unnecessary code style discussions during code reviews, allowing teams to focus on more important aspects of the codebase.</p> <h3> <strong>Combining Husky, ESLint, and Prettier: The Ultimate Team Development Trifecta</strong> </h3> <p>While each of these tools is powerful on its own, combining them can create a seamless and efficient development workflow that ensures code quality and consistency at every step.</p> <p>By combining these powerful tools, you can create a streamlined development workflow that promotes code quality and consistency from the very beginning, reducing technical debt and making it easier for team members to collaborate and maintain the codebase over time.</p> <h3> <strong>Additional Tools and Best Practices</strong> </h3> <p>While Husky, ESLint, and Prettier are powerful tools for enforcing code quality and consistency, they are just a few examples of the many tools and practices that can enhance your team's development workflow.</p> <p>Here are a few additional tools and best practices to consider:</p> <ol> <li><p><strong>TypeScript</strong>: TypeScript is a superset of JavaScript that adds static typing to the language. By using TypeScript, you can catch type-related errors during development, improving code quality and maintainability.</p></li> <li><p><strong>Tailwind CSS</strong>: Tailwind CSS is a utility-first CSS framework that can help teams maintain consistent styling across their projects. It provides a set of pre-defined utility classes that can be used to style components, reducing the need for custom CSS and promoting a more consistent UI.</p></li> <li><p><strong>Folder Structure and Organization</strong>: Maintaining a well-organized and consistent folder structure is crucial for large projects with multiple team members. By following best practices for folder structure and organization, you can make it easier for developers to navigate and understand the codebase.</p></li> <li><p><strong>Pair Programming</strong>: Pair programming is a practice where two developers work together on the same code, taking turns as the "driver" (who writes the code) and the "navigator" (who reviews and provides guidance). This practice can help catch issues early, promote knowledge sharing, and ensure that code is written following agreed-upon standards and best practices.</p></li> <li><p><strong>Version Control</strong>: Using a version control system like Git is essential for team collaboration and maintaining a clear history of code changes. By following best practices for branching strategies, pull requests, and merge workflows, teams can ensure that code changes are reviewed, tested, and merged consistently.</p></li> </ol> <h3> <strong>Real-World Applications and Benefits</strong> </h3> <p>The benefits of using tools like Husky, ESLint, and Prettier, and implementing best practices for code quality and consistency are not just theoretical; they have been proven time and again in real-world projects.</p> <blockquote> <p>At @<a href="https://dev.to@creowis/">CreoWis</a> , we developed an open-source template repository using Next.js, TypeScript, Tailwind CSS, Husky, Prettier, and ESLint. This repository serves as a starting point for new projects, ensuring that they follow best practices and have a solid foundation for code quality and consistency from the very beginning.<br><br> You can check the repository to set up a scaffold for your project using this template repository. 👉 <a href="https://github.com/CreoWis/next-js-launchpad" rel="noopener noreferrer">next-js-launchpad</a></p> </blockquote> <p>By incorporating these tools and practices into our development workflow, we've experienced significant improvements in code maintainability, reduced technical debt, and increased developer productivity.</p> <p>Additionally, the combination of pre-commit hooks, code linting, and formatting has significantly reduced the time spent on code reviews, as many potential issues and style inconsistencies are caught and addressed before the code is even committed.</p> <h3> <strong>Conclusion</strong> </h3> <p>Maintaining code quality and consistency in team development environments is crucial for long-term project success. By leveraging tools like Husky, ESLint, and Prettier, and implementing best practices such as code reviews, and pair programming, teams can streamline their development workflow, reduce technical debt, and ensure a consistently high standard of code quality.</p> <p>The benefits of these practices are not limited to just improved code maintainability and developer productivity. They also contribute to a more collaborative and efficient team environment, where developers can focus on delivering high-quality features without being bogged down by inconsistencies or preventable issues.</p> <p>We encourage you to explore these tools and practices in your own projects.</p> <h3> <strong>Call to Action</strong> </h3> <p>If you found this blog post informative and valuable, we encourage you to follow our blog for more development tips, tutorials, and best practices.</p> <p>Remember, code quality and consistency are not just nice-to-haves; they are essential components of successful software development, especially in team environments. By embracing the right tools and practices, you can set your team up for long-term success and deliver high-quality software that meets the evolving needs of your users.</p> <p>We at <a href="https://creowis.com/" rel="noopener noreferrer"><strong>CreoWis</strong></a> believe in sharing knowledge publicly to help the developer community grow. Let’s collaborate, ideate, and craft passion to deliver awe-inspiring product experiences to the world.</p> <p>Let's connect:</p> <ul> <li><p><a href="https://twitter.com/creowistech" rel="noopener noreferrer">X/Twitter</a></p></li> <li><p><a href="https://www.linkedin.com/company/creowis/" rel="noopener noreferrer">LinkedIn</a></p></li> <li><p><a href="https://creowis.com/" rel="noopener noreferrer">Website</a></p></li> </ul> <blockquote> <p>This article is crafted by <strong>Chhakuli Zingare</strong>, a passionate developer at CreoWis. You can reach out to her on <a href="https://twitter.com/ChhakuliZingare" rel="noopener noreferrer">X/Twitter</a>, <a href="https://www.linkedin.com/in/chhakuli-zingare-322986234/" rel="noopener noreferrer"><strong>LinkedIn</strong></a><strong>,</strong> and follow her work on the <a href="https://github.com/chhakuli123" rel="noopener noreferrer"><strong>GitHub</strong></a><strong>.</strong></p> </blockquote></div>
<div><p><em>I wrote this article as a result of a discussion (I won't call it an argument because it occurred in a learning platform and we're all learning) - React is a Library, or React is a framework. I've been working with React for quite sometime and all the Devs I've worked with usually just call it a framework. And personally, I see it as a framework myself. I was surprised to see some folks saying that's wrong and it's just a Library. This article I believe should do a bit of expo to that, and if you have any reservation, feel free to comment.</em></p> <p>In the world of JavaScript, one debate that continues to persist is whether React is a library or a framework. Officially, React is described by its creators at Facebook (now Meta) as a "JavaScript library for building user interfaces." However, if you've heard someone call it a framework, they aren't necessarily wrong. In this article, we’ll break down the reasons why React can be seen as both a library and a framework, and clarify the difference between a JavaScript library and a JavaScript framework.</p> <p><strong>What is a JavaScript Library?</strong></p> <p>A <strong>JavaScript library</strong> is a collection of pre-written JavaScript code that helps developers solve common problems and build features more easily. A library provides tools to perform specific tasks, but it leaves the developer in control of the application’s structure and architecture.</p> <p>For example:</p> <ul> <li> <strong>React</strong> is often referred to as a UI library because it allows developers to build reusable UI components without dictating how the rest of the application should be built.</li> <li> <strong>Lodash</strong> is a popular JavaScript utility library that provides functions for tasks like working with arrays, objects, and strings. Lodash doesn’t dictate how your project should be structured; it just provides tools you can use where necessary.</li> </ul> <p>In a library, <strong>the developer calls the library</strong>. This means the developer decides when and where to use the library's functions.</p> <p><strong>What is a JavaScript Framework?</strong></p> <p>A <strong>JavaScript framework</strong>, on the other hand, is a full-fledged toolset that dictates much more about how you should build your application. Frameworks provide a structured and opinionated approach to development, offering a complete solution for building applications.</p> <p>In a framework, <strong>the framework calls the developer’s code</strong>, not the other way around. This distinction is referred to as "inversion of control." Frameworks provide default configurations, and the developer fills in the gaps by writing specific logic where needed.</p> <p>For example:</p> <ul> <li> <strong>Angular</strong> is a framework that enforces a particular structure and pattern, such as using components, services, and a module system.</li> <li> <strong>Vue.js</strong>, although lighter and more flexible, still qualifies as a framework because it provides an end-to-end solution for building user interfaces and applications.</li> </ul> <p>Frameworks are often seen as being "opinionated" because they come with rules about how you should structure your code, enforce patterns, and offer tools to handle routing, state management, and more.</p> <p><strong>Why React is a Library</strong></p> <p>React is officially described as a library because:</p> <ul> <li> It is focused on one specific task: <strong>building user interfaces</strong>.</li> <li> React does not impose a specific structure or architecture on your application. You can use React as little or as much as you need without adopting an entire ecosystem or specific conventions.</li> <li> Unlike frameworks like Angular or Vue, React doesn’t come with built-in tools for routing, state management, or making HTTP requests. Developers can add these features by using additional libraries (like React Router for routing or Redux for state management).</li> </ul> <p>Essentially, React provides components and a way to render them efficiently. It doesn’t care how the rest of your application is organized, making it more flexible and modular than a traditional framework.</p> <p><strong>Why React is also a Framework</strong></p> <p>While React is a library at its core, calling it a <strong>framework</strong> is not entirely wrong either. Here’s why:</p> <p><strong>React Ecosystem</strong>:</p> <ul> <li>In practice, many developers use React alongside additional libraries and tools to build full applications, including React Router (for navigation), Redux (for state management), and Next.js (for server-side rendering). These tools combined create a full-fledged development experience, much like a traditional framework.</li> <li>The React ecosystem has grown to the point where you can build entire applications using only React-centric tools. Once you're immersed in the React way of doing things, it can feel just like using a framework.</li> </ul> <p><strong>Inversion of Control</strong>:</p> <ul> <li>While React doesn’t force you into a particular architecture, it does take control over rendering the UI and managing component states. This "inversion of control" characteristic is a hallmark of frameworks.</li> <li>React components dictate when they render and how their states change over time. The React lifecycle methods, hooks like <code>useEffect</code>, and the virtual DOM management are part of how React controls parts of your application.</li> </ul> <p><strong>Developer Experience</strong>:</p> <ul> <li>The fact that React often requires the use of a set of libraries (often React-specific) to build a complete application puts it in a similar category as a framework. When most developers think about using React, they are thinking not just about the core library but about the surrounding ecosystem of tools.</li> <li>Popular frameworks like Next.js, which is built on top of React, offer an all-encompassing structure that feels very framework-like, giving developers routing, server-side rendering, and static site generation—all using React.</li> </ul> <p><strong>Community and Conventions</strong>:</p> <ul> <li>Over the years, the React community has developed conventions and best practices around React development, such as the use of hooks, JSX, and component-based architecture. These conventions help streamline development and make React behave like a framework by providing guidance on how applications should be structured.</li> </ul> <p><strong>Full Application Development</strong>:</p> <ul> <li>With the introduction of tools like <strong>React Native</strong> (for mobile apps) and <strong>Next.js</strong> (for server-side rendered React apps), React has evolved beyond just a UI library. These tools allow developers to build full applications across different platforms, which aligns more with what frameworks traditionally offer.</li> </ul> <p><strong>Library vs. Framework: <em>A Grey Area</em></strong></p> <p>The line between <strong>library</strong> and <strong>framework</strong> isn’t always clear. As we’ve seen with React, a tool can exhibit characteristics of both depending on how it's used. This grey area is common in modern JavaScript development, where the ecosystem is modular, and tools can grow beyond their original purposes.</p> <ul> <li> React, as a core tool, is a <strong>library</strong> that focuses on UI.</li> <li> However, the ecosystem around React and its conventions provide the <strong>framework-like</strong> structure for building full applications.</li> <li> When someone calls React a framework, they’re acknowledging the broad scope and ecosystem React offers, which resembles the capabilities of a framework.</li> </ul> <p><strong>Final Thoughts</strong></p> <p>Whether you call React a <strong>library</strong> or a <strong>framework</strong> depends largely on how you use it and the surrounding tools. What’s most important is recognizing that React can scale from a lightweight, flexible UI library to a powerful tool that acts as a framework in real-world applications.</p> <p>So next time someone tells you React is "just a library," feel free to point out that it might just be a framework, too—depending on how you look at it.</p></div>
<div><p>Next.js is one of the most popular frameworks for building modern web applications. Built on top of React.js, Next.js offers server-side rendering, static site generation, file-based routing, and other powerful features to streamline the web development process.</p> <p><strong>1. What is Next.js?</strong></p> <p>Before diving into the setup, it’s crucial to understand what makes Next.js different from other React frameworks:</p> <ul> <li>Server-Side Rendering (SSR): Pages are pre-rendered on the server, enhancing performance and SEO.</li> <li>Static Site Generation (SSG): Pre-renders pages at build time for lightning-fast performance.</li> <li>File-Based Routing: Routes are automatically generated based on the files in the pages directory.</li> <li>API Routes: Define backend endpoints directly within the same Next.js project.</li> <li>Full Stack Capabilities: Supports hybrid static & server-side rendering, with a flexible deployment system.</li> </ul> <p><strong>2. Setting Up a Next.js Project</strong></p> <p>Next.js is constantly evolving, and in 2024, it’s easier than ever to set up and start building your application.</p> <p><strong>Step 1: Prerequisites</strong></p> <p>Ensure that you have the following prerequisites installed:</p> <p>Node.js: Version 18.x or higher. You can check your version by running:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight shell"><code>node <span class="nt">-v</span> </code></pre> </div> <p>If you don't have Node.js, download it from <a href="https://www.nodejs.org/" rel="noopener noreferrer">nodejs.org</a>.</p> <p><strong>npm or yarn:</strong> npm comes bundled with Node.js, but you can also use yarn as a package manager if preferred.</p> <p><strong>Step 2: Creating a New Next.js Project</strong></p> <p>Next.js provides a command-line tool called create-next-app that streamlines the setup process.</p> <ol> <li>Navigate to the folder where you want to create your project: </li> </ol> <div class="highlight js-code-highlight"> <pre class="highlight shell"><code><span class="nb">cd</span> /path/to/your/directory </code></pre> </div> <ol> <li>Run the command to create a Next.js app: If you're using <code>npm</code>, run: </li> </ol> <div class="highlight js-code-highlight"> <pre class="highlight shell"><code>npx create-next-app@latest </code></pre> </div> <p>For <strong>yarn</strong>, use:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight shell"><code>yarn create next-app </code></pre> </div> <p><strong>Follow the prompts:</strong></p> <ul> <li> <strong>Project Name:</strong> Give your project a name (e.g., nextjs-app).</li> <li> <strong>Typescript:</strong> Decide whether to use TypeScript or JavaScript.</li> <li> <strong>ESLint:</strong> Enable or disable linting (recommended to enable).</li> <li> <strong>Tailwind CSS:</strong> You can choose to add Tailwind CSS during the setup, or add it later manually.</li> <li> <strong>Import Alias:</strong> Decide whether to configure import aliases for cleaner file imports.</li> </ul> <p>After this, the Next.js starter project will be created with all the essential configurations.</p> <p><strong>Step 3: Exploring the Folder Structure</strong></p> <p>Once your project is created, the structure will look like this:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight shell"><code>nextjs-app/ ├── node_modules/ ├── pages/ │ ├── api/ │ │ └── hello.js │ ├── _app.js │ ├── index.js ├── public/ ├── styles/ │ ├── globals.css │ ├── Home.module.css ├── .eslintrc.json ├── next.config.js ├── package.json └── README.md </code></pre> </div> <ul> <li> <strong>pages/:</strong> This is where the routes and page components are defined.</li> <li> <strong>pages/api/:</strong> API routes are stored here; <code>hello.js</code> is an example.</li> <li> <strong>public/:</strong> Static assets (e.g., images, fonts) are placed here.</li> <li> <strong>styles/:</strong> Global and module-specific stylesheets.</li> </ul> <p><strong>Step 4: Running the Development Server</strong></p> <p>Navigate into your project directory:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight shell"><code> <span class="nb">cd </span>nextjs-app </code></pre> </div> <p>Start the development server:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight shell"><code> npm run dev </code></pre> </div> <p>or<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight shell"><code> yarn dev </code></pre> </div> <p>This will start the Next.js server at <a href="http://localhost:3000/" rel="noopener noreferrer">http://localhost:3000</a>. You can now open your browser and visit <a href="http://localhost:3000/" rel="noopener noreferrer">http://localhost:3000</a> to see the default Next.js page.</p> <p><strong>3. Core Features of Next.js</strong></p> <p>A. File-Based Routing</p> <p>Next.js uses a file-based routing system, meaning every file in the <code>pages/</code> directory automatically becomes a route.</p> <ul> <li>Create a new page:</li> </ul> <p>Create a new file called about.js inside the <code>pages/</code> directory:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight shell"><code> <span class="nb">touch </span>pages/about.js </code></pre> </div> <ul> <li>Add the following content to the file: </li> </ul> <div class="highlight js-code-highlight"> <pre class="highlight jsx"><code> <span class="kd">const</span> <span class="nx">About</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="k">return</span> <span class="p"><</span><span class="nt">h1</span><span class="p">></span>About Us<span class="p"></</span><span class="nt">h1</span><span class="p">>;</span> <span class="p">}</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">About</span><span class="p">;</span> </code></pre> </div> <p>Access the page: Open your browser and visit <a href="http://localhost:3000/about" rel="noopener noreferrer">http://localhost:3000/about</a>, and you’ll see the content from the About component.</p> <p><strong>B. Linking Between Pages</strong></p> <p>Next.js comes with a built-in Link component to handle client-side navigation.</p> <p>Modify your index.js file to include a link to the About page:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight jsx"><code> <span class="k">import</span> <span class="nx">Link</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">next/link</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">Home</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="k">return </span><span class="p">(</span> <span class="p"><</span><span class="nt">div</span><span class="p">></span> <span class="p"><</span><span class="nt">h1</span><span class="p">></span>Welcome to the Home Page<span class="p"></</span><span class="nt">h1</span><span class="p">></span> <span class="p"><</span><span class="nc">Link</span> <span class="na">href</span><span class="p">=</span><span class="s">"/about"</span><span class="p">></span>Go to About Page<span class="p"></</span><span class="nc">Link</span><span class="p">></span> <span class="p"></</span><span class="nt">div</span><span class="p">></span> <span class="p">);</span> <span class="p">};</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">Home</span><span class="p">;</span> </code></pre> </div> <p>Now, when you click on the "Go to About Page" link, you will be routed to the about page.</p> <p><strong>C. CSS and Styling in Next.js</strong></p> <p>Next.js allows you to import CSS files globally or use CSS modules for component-level styles.</p> <p><strong>Global CSS:</strong></p> <p>In the styles/globals.css file, you can add global styles:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight css"><code> <span class="nt">body</span> <span class="p">{</span> <span class="nl">font-family</span><span class="p">:</span> <span class="n">Arial</span><span class="p">,</span> <span class="nb">sans-serif</span><span class="p">;</span> <span class="nl">margin</span><span class="p">:</span> <span class="m">0</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p>Import this file in your _app.js component:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight jsx"><code> <span class="k">import</span> <span class="dl">'</span><span class="s1">../styles/globals.css</span><span class="dl">'</span><span class="p">;</span> <span class="kd">function</span> <span class="nf">MyApp</span><span class="p">({</span> <span class="nx">Component</span><span class="p">,</span> <span class="nx">pageProps</span> <span class="p">})</span> <span class="p">{</span> <span class="k">return</span> <span class="p"><</span><span class="nc">Component</span> <span class="si">{</span><span class="p">...</span><span class="nx">pageProps</span><span class="si">}</span> <span class="p">/>;</span> <span class="p">}</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">MyApp</span><span class="p">;</span> </code></pre> </div> <p>CSS Modules: For component-specific styles, Next.js supports CSS Modules. Modify Home.module.css:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight css"><code> <span class="nc">.title</span> <span class="p">{</span> <span class="nl">color</span><span class="p">:</span> <span class="no">red</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p>Then, import it in your index.js file:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight jsx"><code> <span class="k">import</span> <span class="nx">styles</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">../styles/Home.module.css</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">Home</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="k">return</span> <span class="p"><</span><span class="nt">h1</span> <span class="na">className</span><span class="p">=</span><span class="si">{</span><span class="nx">styles</span><span class="p">.</span><span class="nx">title</span><span class="si">}</span><span class="p">></span>Welcome to the Home Page<span class="p"></</span><span class="nt">h1</span><span class="p">>;</span> <span class="p">};</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">Home</span><span class="p">;</span> </code></pre> </div> <p><strong>4. Data Fetching in Next.js</strong></p> <p>Next.js offers multiple ways to fetch data for pages.</p> <p><strong>A. Static Site Generation (SSG)</strong></p> <p>Pre-render the page at build time by using the getStaticProps function:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight jsx"><code> <span class="k">export</span> <span class="k">async</span> <span class="kd">function</span> <span class="nf">getStaticProps</span><span class="p">()</span> <span class="p">{</span> <span class="kd">const</span> <span class="nx">res</span> <span class="o">=</span> <span class="k">await</span> <span class="nf">fetch</span><span class="p">(</span><span class="dl">'</span><span class="s1">https://api.example.com/data</span><span class="dl">'</span><span class="p">);</span> <span class="kd">const</span> <span class="nx">data</span> <span class="o">=</span> <span class="k">await</span> <span class="nx">res</span><span class="p">.</span><span class="nf">json</span><span class="p">();</span> <span class="k">return</span> <span class="p">{</span> <span class="na">props</span><span class="p">:</span> <span class="p">{</span> <span class="nx">data</span> <span class="p">}</span> <span class="p">};</span> <span class="p">}</span> <span class="kd">const</span> <span class="nx">Home</span> <span class="o">=</span> <span class="p">({</span> <span class="nx">data</span> <span class="p">})</span> <span class="o">=></span> <span class="p">{</span> <span class="k">return </span><span class="p">(</span> <span class="p"><</span><span class="nt">div</span><span class="p">></span> <span class="p"><</span><span class="nt">h1</span><span class="p">></span>Data from API:<span class="p"></</span><span class="nt">h1</span><span class="p">></span> <span class="p"><</span><span class="nt">pre</span><span class="p">></span><span class="si">{</span><span class="nx">JSON</span><span class="p">.</span><span class="nf">stringify</span><span class="p">(</span><span class="nx">data</span><span class="p">,</span> <span class="kc">null</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span><span class="si">}</span><span class="p"></</span><span class="nt">pre</span><span class="p">></span> <span class="p"></</span><span class="nt">div</span><span class="p">></span> <span class="p">);</span> <span class="p">};</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">Home</span><span class="p">;</span> </code></pre> </div> <p><strong>B. Server-Side Rendering (SSR)</strong></p> <p>Fetch data at request time using the getServerSideProps function:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight jsx"><code> <span class="k">export</span> <span class="k">async</span> <span class="kd">function</span> <span class="nf">getServerSideProps</span><span class="p">()</span> <span class="p">{</span> <span class="kd">const</span> <span class="nx">res</span> <span class="o">=</span> <span class="k">await</span> <span class="nf">fetch</span><span class="p">(</span><span class="dl">'</span><span class="s1">https://api.example.com/data</span><span class="dl">'</span><span class="p">);</span> <span class="kd">const</span> <span class="nx">data</span> <span class="o">=</span> <span class="k">await</span> <span class="nx">res</span><span class="p">.</span><span class="nf">json</span><span class="p">();</span> <span class="k">return</span> <span class="p">{</span> <span class="na">props</span><span class="p">:</span> <span class="p">{</span> <span class="nx">data</span> <span class="p">}</span> <span class="p">};</span> <span class="p">}</span> <span class="kd">const</span> <span class="nx">Home</span> <span class="o">=</span> <span class="p">({</span> <span class="nx">data</span> <span class="p">})</span> <span class="o">=></span> <span class="p">{</span> <span class="k">return </span><span class="p">(</span> <span class="p"><</span><span class="nt">div</span><span class="p">></span> <span class="p"><</span><span class="nt">h1</span><span class="p">></span>Server-Side Data:<span class="p"></</span><span class="nt">h1</span><span class="p">></span> <span class="p"><</span><span class="nt">pre</span><span class="p">></span><span class="si">{</span><span class="nx">JSON</span><span class="p">.</span><span class="nf">stringify</span><span class="p">(</span><span class="nx">data</span><span class="p">,</span> <span class="kc">null</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span><span class="si">}</span><span class="p"></</span><span class="nt">pre</span><span class="p">></span> <span class="p"></</span><span class="nt">div</span><span class="p">></span> <span class="p">);</span> <span class="p">};</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">Home</span><span class="p">;</span> </code></pre> </div> <p><strong>5. API Routes in Next.js</strong></p> <p>Next.js allows you to define backend routes within the same project. These routes are created inside the <code>pages/api/</code> folder.</p> <p>Create a file called hello.js inside <code>pages/api/</code>:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight javascript"><code> <span class="k">export</span> <span class="k">default</span> <span class="kd">function</span> <span class="nf">handler</span><span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="nx">res</span><span class="p">)</span> <span class="p">{</span> <span class="nx">res</span><span class="p">.</span><span class="nf">status</span><span class="p">(</span><span class="mi">200</span><span class="p">).</span><span class="nf">json</span><span class="p">({</span> <span class="na">message</span><span class="p">:</span> <span class="dl">'</span><span class="s1">Hello from Next.js API!</span><span class="dl">'</span> <span class="p">});</span> <span class="p">}</span> </code></pre> </div> <p>Access the API endpoint at <a href="http://localhost:3000/api/hello" rel="noopener noreferrer">http://localhost:3000/api/hello</a>.</p> <p><strong>6. Deployment in Next.js</strong></p> <p>Next.js projects can be deployed on various platforms like Vercel, Netlify, or any Node.js server. Here's how to deploy on Vercel:</p> <p>Install Vercel CLI:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight shell"><code> npm <span class="nb">install</span> <span class="nt">-g</span> vercel </code></pre> </div> <p>Deploy: From your project directory, run:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight shell"><code> vercel </code></pre> </div> <p>PS: Localhost is not a URL you can access over the internet; it only works on your local computer. It refers to the server running on your machine, typically at <a href="http://localhost/" rel="noopener noreferrer">http://localhost</a>, and is used for development purposes.</p></div>
<div><h2> Why Hire React Developers for Your Next Mobile App Project? </h2> <p>Mobile app development is important for companies looking to grow and engage their audience in the modern rapid digital landscape. Whether you're a startup aiming to launch a new product or an established business seeking to scale, the choice of technology for mobile apps is critical. Right here is where React and React Native come in handy.</p> <p>Hiring React developers has become increasingly popular, especially for businesses that want efficient, scalable, and cutting-edge mobile apps. But the reason is that React is the right choice for Android app development, and why should you <a href="https://disolutions.net/hire-us/hire-dedicated-react-js-developers" rel="noopener noreferrer">hire React developers</a>? Let’s dive into the reasons, benefits, and insights you need to know.</p> <h2> What Is React, and Why Does It Matter? </h2> <p>React is a powerful JavaScript library created by Facebook, mainly used for building user interfaces, especially single-page applications. It allows developers to create reusable UI components, making development faster, more efficient, and scalable. React Native, an extension of React enables developers to create mobile applications for both iOS and Android using a single codebase.</p> <h3> Why React for Mobile App Development? </h3> <p>React Native allows developers to write code once and deploy it across multiple platforms, significantly reducing development time and costs. It’s particularly appealing to businesses that want to build cross-platform mobile apps without compromising on quality or performance.</p> <h2> Benefits of Hiring React Developers for Mobile App Projects </h2> <h3> 1. Cross-Platform Compatibility </h3> <p>One of the biggest reasons to hire React developers is their ability to build apps that work on iOS and Android with a single codebase. This minimises development costs and time while delivering a uniform experience across platforms. React Native, in particular, is known for its seamless integration with native components, providing a near-native app experience.</p> <h3> 2. Faster Time to Market </h3> <p>Time is often of the essence when launching a mobile app. React’s component-based architecture allows developers to reuse code and UI components, speeding up development. You may shorten the time it takes for your app to launch significantly without sacrificing functionality or quality by working with React developers.</p> <h3> 3. Cost-Effective Development </h3> <p>Developing different native apps for iOS and Android could be expensive. Hiring React developers who specialize in React Native means you get two apps for the price of one. Additionally, the ability to share code between web and mobile versions of your app can further cut costs, making React a budget-friendly option for businesses.</p> <h3> 4. Scalability and Flexibility </h3> <p>**React is highly scalable, allowing businesses to start small and expand their app as needed. Whether you're adding new features or growing your app to support more people, React offers the freedom and agility to change quickly. Hiring skilled React developers ensures your app is built with this growth in mind.</p> <h3> 5. Strong Community Support </h3> <p>React has a vast and active developer community. By hiring React developers, you’re tapping into a network of professionals who continuously contribute to its growth. This community-driven development ensures that your app stays updated with the latest features, tools, and best practices.</p> <h2> Key Skills to Look for When You Hire React Developers </h2> <p>To ensure your mobile app project succeeds, you need the right talent. Here are some essential skills to look for when hiring React developers:</p> <h3> 1. Proficiency in JavaScript </h3> <p>React is built on JavaScript, so a deep understanding of this language is essential. Your developer should have hands-on experience with modern JavaScript (ES6+), including concepts like closures, async/await, and arrow functions.</p> <h3> 2. Experience with React Native </h3> <p>If you’re building a mobile app, your developer must have experience with React Native. They should be familiar with its core components, APIs, and libraries, and know how to integrate native modules when necessary.</p> <h3> 3. Knowledge of State Management </h3> <p>Managing the state effectively is crucial for building scalable apps. Developers should be proficient with state management tools such as Redux, MobX, or the Context API.</p> <h3> 4. Familiarity with Testing Frameworks </h3> <p>Quality assurance is vital to app development. Ensure your React developers have experience with testing libraries and tools like Jest, Enzyme, or React Testing Library to create robust, bug-free applications.</p> <h3> 5. UI/UX Design Principles </h3> <p>Hiring a React developer with a solid understanding of UI/UX principles ensures that your app isn’t just functional but also visually appealing and user-friendly.</p> <h2> How to Hire React Developers: In-House vs. Outsourcing </h2> <h3> In-House Development </h3> <p>If you have the budget and long-term plans for app development, hiring React developers as part of your in-house team may be the best option. This allows for greater control over the project and fosters better collaboration among team members.</p> <p>However, developing within the organization team can be costly and costly, especially if you're founded in areas where React skills are in high demand.</p> <h3> Outsourcing Development </h3> <p>Outsourcing allows businesses to hire React developers from anywhere in the world, often at a fraction of the cost. Outsourcing also gives you access to a larger talent pool, ensuring that you find the right developers with the skills you need.</p> <p>This option works best for short-term projects or businesses looking to scale quickly without committing to long-term salaries and overhead costs.</p> <h2> When Should You Hire React Developers? </h2> <p>Hiring React developers is a great option if you’re:</p> <p>**Building a mobile app **that demands compatibility with both Android and iOS.</p> <p><strong>Looking to cut costs</strong> by reusing code between web and mobile platforms.</p> <p><strong>Facing a tight deadline</strong> and need faster development with reusable components.</p> <p><strong>Scaling an existing app</strong> and need a flexible, efficient solution.</p> <p><strong>Exploring new tech features</strong>, such as integrating AR, AI, or IoT capabilities into your app.</p> <p>React is an excellent choice for businesses of all sizes looking to create scalable, high-performance mobile apps. But to unlock its full potential, you need skilled developers who understand both the technical and business aspects of app development.</p> <p>Read More:- <a href="https://dev.to/arjuncodess/react-in-2024-a-beginners-guide-152a">React in 2024: A Beginner's Guide 🌟🚀</a></p> <h2> Final Thoughts: Invest in the Right React Talent </h2> <p>Choosing to <a href="https://disolutions.net/hire-us/hire-dedicated-react-js-developers" rel="noopener noreferrer">hire react developer India</a> for your mobile app project is a smart, strategic decision that can save you time and money. Their expertise with React’s component-based architecture, cross-platform compatibility, and scalability make them an invaluable asset to your business.</p> <p>The demand for React developers is growing, but not all developers are created equal. Make sure to vet potential candidates carefully, ensuring they possess the skills and experience necessary to bring your vision to life.</p> <p>Ready to take your mobile app project to the next level? Hire React developers today to ensure your app’s success and make a lasting impact on your users.</p></div>
<div><p>Are you working on a <strong>React-based admin panel</strong> or dashboard project in 2024? If you're looking for <strong><a href="https://tailgrids.com/react/components#dashboard" rel="noopener noreferrer">React dashboard components</a></strong> to create <strong>data-rich</strong>, interactive, and scalable applications, your search ends here!</p> <p><strong><a href="https://tailgrids.com/react" rel="noopener noreferrer">TailGrids React</a></strong> - Dashboard Components is the ultimate toolkit designed to help developers effortlessly build modern, user-friendly, and powerful dashboards. With over <strong>100+ components</strong> for admin panels, this toolkit equips you to create fully responsive and highly customizable backend interfaces in no time.</p> <h2> Why to TailGrids React Dashboard Components? </h2> <p><a href="https://tailgrids.com/react/components" rel="noopener noreferrer"><img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk43wrbnugyxgfajehsy1.png" alt="React Dashboard Components" width="" height=""></a><br> Building a professional-grade dashboard requires a <strong>comprehensive set of UI components</strong> that are flexible, optimized, and scalable. </p> <p>TailGrids delivers all this, allowing you to save time while ensuring top-notch performance and user experience. </p> <p>Here's why TailGrids is the go-to choice in <strong>2024</strong>:</p> <ul> <li> <strong>100+ Tailwind CSS React Components</strong>: With everything from graphs and charts to calendars and dropdowns, TailGrids has all essential elements to power your dashboards.</li> <li> <strong>Performance Optimized</strong>: Built with <strong><a href="https://react.dev/" rel="noopener noreferrer">React</a></strong> and <strong><a href="https://tailwindcss.com/" rel="noopener noreferrer">Tailwind CSS</a></strong>, the components are optimized for fast load times and smooth interactions, critical for a great user experience (UX).</li> <li> <strong>Scalable & Responsive</strong>: TailGrids is adaptable whether you're building a small admin dashboard or an enterprise-level solution. The responsive design ensures seamless functionality across all device types.</li> <li> <strong>Figma Source File</strong>: Offers a complete <a href="https://tailgrids.com/figma" rel="noopener noreferrer">Figma design source</a> file to experiment, prototype, or adjust according to your requirements.</li> </ul> <h2> Detailed Overview of TailGrids Dashboard Components </h2> <p><a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg5fmp2x63rf1wabfqc47.png" class="article-body-image-wrapper"><img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg5fmp2x63rf1wabfqc47.png" alt="React Dashboard UI Kit" width="" height=""></a><br> Let's take a closer look at the powerful components TailGrids React offers for admin dashboards:</p> <ul> <li> <strong>Calendars</strong> (4 Components): Easily integrate customizable calendar components for scheduling, task management, and event tracking.</li> </ul> <p><a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fomlfrqst3o4d8jr2kwaa.png" class="article-body-image-wrapper"><img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fomlfrqst3o4d8jr2kwaa.png" alt="React Calendar Components" width="" height=""></a></p> <ul> <li> <strong>Charts</strong> (10 Components): Create data-rich dashboards with a variety of chart components, including bar, line, and pie charts for data visualization. <img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F62poi70p8jn4ozrzws86.png" alt="React Charts Components" width="" height=""> </li> <li> <strong>Chat Boxes</strong> (4 Components): Enable real-time communication with ready-to-use chat box components. Perfect for applications requiring live messaging or customer support features.</li> </ul> <p><a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8locto3alwbnlxg4hpi7.png" class="article-body-image-wrapper"><img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8locto3alwbnlxg4hpi7.png" alt="React Chat Components" width="" height=""></a></p> <ul> <li> <strong>Chat Lists</strong> (3 Components): Display and manage conversations with customizable chat lists, making communication easy and intuitive.</li> <li> <strong>Dashboard Dropdowns</strong> (3 Components): Utilize sleek and user-friendly dropdown components, ensuring smooth interaction for dashboard users.</li> <li><p><strong>Data Stats</strong> (10 Components): Showcase key performance indicators (KPIs) or metrics through data stats components. Ideal for financial dashboards or performance tracking systems.<br> <a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmyamx2la1luytvsirer5.png" class="article-body-image-wrapper"><img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmyamx2la1luytvsirer5.png" alt="Data Stats Components" width="" height=""></a></p></li> <li><p><strong>Drawers</strong> (2 Components): Keep your interface clutter-free with side drawers that can store settings, filters, or notifications.</p></li> <li><p><strong>Horizontal Menus</strong> (6 Components): Organize navigation with horizontal menus, perfect for creating intuitive dashboard navigation experiences.</p></li> <li><p><strong>Maps</strong> (4 Components): Incorporate interactive and responsive maps for geographical data visualization, making it a core feature in logistics or tracking systems.</p></li> </ul> <p><a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F20uxiaj886i263lu97zk.png" class="article-body-image-wrapper"><img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F20uxiaj886i263lu97zk.png" alt="React Map Component" width="" height=""></a></p> <ul> <li> <strong>Popovers</strong> (6 Components): Provide additional context with interactive popover components to offer tooltips, quick actions, or extra information.</li> <li> <strong>Profiles</strong> (5 Components): Easily manage and display user information, activity, and settings with customizable profile components.</li> </ul> <p><a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftrqkkm7f9mb4aql1zrue.png" class="article-body-image-wrapper"><img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftrqkkm7f9mb4aql1zrue.png" alt="React Profile Component" width="" height=""></a></p> <ul> <li> <strong>Select Box</strong> (4 Components): Add interactive select boxes to provide dropdown-style options for filtering or selecting data in your dashboard.</li> <li> <strong>Settings Pages</strong> (2 Components): Create intuitive and well-organized settings pages to enable users to configure and customize their preferences.</li> <li><p><strong>Steps</strong> (8 Components): Use multi-step components for process flows like user onboarding or checkout experiences.<br> <a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftugcalsiuyqfhfv5i22e.png" class="article-body-image-wrapper"><img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftugcalsiuyqfhfv5i22e.png" alt="Steps Components" width="" height=""></a></p></li> <li><p><strong>Table Stacks</strong> (5 Components): Display large datasets with responsive table components that are optimized for reading and navigation across devices.</p></li> <li><p><strong>Vertical Navbars</strong> (7 Components): Organize your dashboard's navigation using vertical navigation bars, ensuring smooth user navigation across various sections of your app.</p></li> </ul> <h3> Pre-Built Admin Templates for Quick Setup </h3> <p><a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbilbi42h4entnjfyol31.png" class="article-body-image-wrapper"><img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbilbi42h4entnjfyol31.png" alt="React Dashboard Templates" width="" height=""></a><br> In addition to 100+ individual components, TailGrids provides <strong>4 pre-built <a href="https://tailgrids.com/templates" rel="noopener noreferrer">admin/dashboard templates</a></strong> designed for a <strong>quick and easy setup</strong>. These templates are crafted with React and Tailwind CSS and come equipped with pre-built <a href="https://tailgrids.com/dashboard-components" rel="noopener noreferrer">dashboard UI components </a>such as <strong>charts, tables, forms,</strong> and more.</p> <p><strong>Ready-to-use templates</strong> include:</p> <ul> <li>1. <strong><a href="https://tailgrids.com/templates/admino-tailwind-dashboard-and-admin-template" rel="noopener noreferrer">Admino</a></strong> – Tailwind React Dashboard Template</li> <li>2. <strong><a href="https://tailgrids.com/templates/levels-tailwind-e-commerce-dashboard-template" rel="noopener noreferrer">Levels</a></strong> – React E-Commerce Dashboard Template</li> <li>3. <strong><a href="https://tailgrids.com/templates/dashy-tailwind-css-dashboard-template" rel="noopener noreferrer">Dashy</a></strong> – React Admin Template</li> <li>4. <strong><a href="https://tailgrids.com/templates/mega-minimal-tailwind-css-dashboard-template" rel="noopener noreferrer">Mega</a></strong> – Minimal Tailwind CSS React Dashboard Template</li> </ul> <p>Whether you're working on a small dashboard project or a complex admin solution, these templates offer a solid foundation to get you started faster.</p> <h2> Conclusion </h2> <p>TailGrids has continued to evolve, ensuring that in <strong>2024</strong>, it remains a top solution for developers building <strong>React admin dashboards</strong>. It offers over 100+ essential components and 4 ready-to-use templates. <br> <a href="https://tailgrids.com/react/components" rel="noopener noreferrer"><img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5u3ef9r4zpj4dc4k470s.png" alt="100+ React Dashboard Components" width="" height=""></a><br> Even the most popular <strong>Tailwind CSS Admin Dashboard Panel</strong> - <a href="https://tailadmin.com/react" rel="noopener noreferrer">TailAdmin </a>is built with TailGrids. With this toolkit, you can create scalable, data-driven, and visually stunning applications quickly and easily.</p> <p>Start building your React-based admin dashboard with <strong><a href="https://tailgrids.com/react" rel="noopener noreferrer">TailGrids React </a></strong>today!</p></div>
Customize keyboard shortcuts on Chromebook with these simple steps. Enhance productivity by creating personalized key ...
While human connections and relationships are important to building trust, clear expectations and autonomy become crucial to ...
<div><h2> 🙋ðŸ»â€â™€ï¸ Introduction </h2> <p>TypeScript adds static typing to JavaScript, which helps catch potential bugs before they even run. When paired with React, TypeScript enhances your components by enforcing type safety. In this article, we'll walk through the essential concepts for using TypeScript with React.</p> <h2> âš›ï¸ How to Use TypeScript with React? </h2> <p>First, you need to install TypeScript in a React project. If you're starting from scratch, create a new React app with TypeScript enabled:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight shell"><code>npx create-react-app my-app <span class="nt">--template</span> typescript </code></pre> </div> <p>For an existing project, you can add TypeScript by running:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight shell"><code>npm <span class="nb">install </span>typescript @types/react @types/react-dom </code></pre> </div> <p>Now, you can start using <code>.tsx</code> files instead of <code>.js</code> or <code>.jsx</code>, and your React components will benefit from TypeScript’s features.</p> <h2> Types for React Component Props </h2> <p>When you’re defining a component in React, you can specify the types for its props to make the usage of the component clearer and safer. Here’s a basic example:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight tsx"><code><span class="kd">type</span> <span class="nx">UserProps</span> <span class="o">=</span> <span class="p">{</span> <span class="na">name</span><span class="p">:</span> <span class="kr">string</span><span class="p">;</span> <span class="nl">age</span><span class="p">:</span> <span class="kr">number</span><span class="p">;</span> <span class="p">};</span> <span class="kd">const</span> <span class="nx">UserCard</span> <span class="o">=</span> <span class="p">({</span> <span class="nx">name</span><span class="p">,</span> <span class="nx">age</span> <span class="p">}:</span> <span class="nx">UserProps</span><span class="p">)</span> <span class="o">=></span> <span class="p">(</span> <span class="p"><</span><span class="nt">div</span><span class="p">></span> <span class="p"><</span><span class="nt">h1</span><span class="p">></span><span class="si">{</span><span class="nx">name</span><span class="si">}</span><span class="p"></</span><span class="nt">h1</span><span class="p">></span> <span class="p"><</span><span class="nt">p</span><span class="p">></span><span class="si">{</span><span class="nx">age</span><span class="si">}</span> years old<span class="p"></</span><span class="nt">p</span><span class="p">></span> <span class="p"></</span><span class="nt">div</span><span class="p">></span> <span class="p">);</span> </code></pre> </div> <p>In this example, <code>UserCard</code> expects two props: <code>name</code> (a string) and <code>age</code> (a number). If you try to pass incorrect types, TypeScript will show an error.</p> <h2> ReactNode Vs ReactElement </h2> <p>You might wonder when to use <code>ReactNode</code> or <code>ReactElement</code> for typing React components.</p> <ul> <li><p><strong>ReactNode</strong>: Represents anything that can be rendered by React. This includes strings, numbers, JSX, arrays, and <code>null</code>.</p></li> <li><p><strong>ReactElement</strong>: Refers to an actual React element, which is more specific and doesn’t cover strings or <code>null</code>.</p></li> </ul> <p>Example:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight tsx"><code><span class="kd">type</span> <span class="nx">CardProps</span> <span class="o">=</span> <span class="p">{</span> <span class="na">children</span><span class="p">:</span> <span class="nx">ReactNode</span><span class="p">;</span> <span class="p">};</span> <span class="kd">const</span> <span class="nx">Card</span> <span class="o">=</span> <span class="p">({</span> <span class="nx">children</span> <span class="p">}:</span> <span class="nx">CardProps</span><span class="p">)</span> <span class="o">=></span> <span class="p"><</span><span class="nt">div</span><span class="p">></span><span class="si">{</span><span class="nx">children</span><span class="si">}</span><span class="p"></</span><span class="nt">div</span><span class="p">>;</span> <span class="c1">// Usage</span> <span class="p"><</span><span class="nc">Card</span><span class="p">><</span><span class="nt">p</span><span class="p">></span>Hello, World!<span class="p"></</span><span class="nt">p</span><span class="p">></</span><span class="nc">Card</span><span class="p">></span> </code></pre> </div> <p><code>ReactNode</code> is used here because <code>children</code> can be any renderable element, not just React elements.</p> <h2> Type Vs Interface </h2> <p>When defining the shape of props or other objects, both <code>type</code> and <code>interface</code> can be used. So, what’s the difference?</p> <ul> <li> <strong>Type</strong>: Best for creating union types or more complex combinations.</li> <li> <strong>Interface</strong>: Often better when you plan to extend or implement additional types.</li> </ul> <p>Here’s a comparison:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight tsx"><code><span class="c1">// Using type</span> <span class="kd">type</span> <span class="nx">ButtonProps</span> <span class="o">=</span> <span class="p">{</span> <span class="na">label</span><span class="p">:</span> <span class="kr">string</span><span class="p">;</span> <span class="nl">onClick</span><span class="p">:</span> <span class="p">()</span> <span class="o">=></span> <span class="k">void</span><span class="p">;</span> <span class="p">};</span> <span class="c1">// Using interface</span> <span class="kr">interface</span> <span class="nx">LinkProps</span> <span class="p">{</span> <span class="nl">href</span><span class="p">:</span> <span class="kr">string</span><span class="p">;</span> <span class="nl">openInNewTab</span><span class="p">?:</span> <span class="nx">boolean</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p>While both are similar, <code>interface</code> can be extended more naturally:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight tsx"><code><span class="kr">interface</span> <span class="nx">IconButtonProps</span> <span class="kd">extends</span> <span class="nx">ButtonProps</span> <span class="p">{</span> <span class="nl">icon</span><span class="p">:</span> <span class="kr">string</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <h2> How to Type Props with TypeScript? </h2> <p>To type props for a component, declare a <code>type</code> or <code>interface</code> and pass it as the type annotation for the component's props.</p> <p>Example:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight tsx"><code><span class="kd">type</span> <span class="nx">AlertProps</span> <span class="o">=</span> <span class="p">{</span> <span class="na">message</span><span class="p">:</span> <span class="kr">string</span><span class="p">;</span> <span class="nl">severity</span><span class="p">:</span> <span class="dl">'</span><span class="s1">error</span><span class="dl">'</span> <span class="o">|</span> <span class="dl">'</span><span class="s1">warning</span><span class="dl">'</span> <span class="o">|</span> <span class="dl">'</span><span class="s1">info</span><span class="dl">'</span><span class="p">;</span> <span class="p">};</span> <span class="kd">const</span> <span class="nx">Alert</span> <span class="o">=</span> <span class="p">({</span> <span class="nx">message</span><span class="p">,</span> <span class="nx">severity</span> <span class="p">}:</span> <span class="nx">AlertProps</span><span class="p">)</span> <span class="o">=></span> <span class="p">(</span> <span class="p"><</span><span class="nt">div</span> <span class="na">className</span><span class="p">=</span><span class="si">{</span><span class="s2">`alert </span><span class="p">${</span><span class="nx">severity</span><span class="p">}</span><span class="s2">`</span><span class="si">}</span><span class="p">></span><span class="si">{</span><span class="nx">message</span><span class="si">}</span><span class="p"></</span><span class="nt">div</span><span class="p">></span> <span class="p">);</span> </code></pre> </div> <h2> How to Type Functional Component Props? </h2> <p>To type a functional component's props, you can use <code>React.FC<Props></code> or declare the props explicitly. While <code>React.FC</code> was commonly used, now many prefer explicit types.</p> <p>Here’s an example using explicit types:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight tsx"><code><span class="kd">type</span> <span class="nx">BadgeProps</span> <span class="o">=</span> <span class="p">{</span> <span class="na">text</span><span class="p">:</span> <span class="kr">string</span><span class="p">;</span> <span class="nl">color</span><span class="p">:</span> <span class="kr">string</span><span class="p">;</span> <span class="p">};</span> <span class="kd">const</span> <span class="nx">Badge</span> <span class="o">=</span> <span class="p">({</span> <span class="nx">text</span><span class="p">,</span> <span class="nx">color</span> <span class="p">}:</span> <span class="nx">BadgeProps</span><span class="p">)</span> <span class="o">=></span> <span class="p">(</span> <span class="p"><</span><span class="nt">span</span> <span class="na">style</span><span class="p">=</span><span class="si">{</span><span class="p">{</span> <span class="na">backgroundColor</span><span class="p">:</span> <span class="nx">color</span> <span class="p">}</span><span class="si">}</span><span class="p">></span><span class="si">{</span><span class="nx">text</span><span class="si">}</span><span class="p"></</span><span class="nt">span</span><span class="p">></span> <span class="p">);</span> </code></pre> </div> <p>In this example, <code>Badge</code> has two props, <code>text</code> and <code>color</code>, both typed as strings.</p> <h2> ⛓ React Hooks with TypeScript </h2> <p>React hooks can also be typed in TypeScript to ensure type safety for the state and effect logic in your components.</p> <p>Here’s how to use <code>useState</code> and <code>useEffect</code> with TypeScript:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight tsx"><code><span class="k">import</span> <span class="p">{</span> <span class="nx">useState</span><span class="p">,</span> <span class="nx">useEffect</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">Counter</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="kd">const</span> <span class="p">[</span><span class="nx">count</span><span class="p">,</span> <span class="nx">setCount</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="o"><</span><span class="kr">number</span><span class="o">></span><span class="p">(</span><span class="mi">0</span><span class="p">);</span> <span class="nf">useEffect</span><span class="p">(()</span> <span class="o">=></span> <span class="p">{</span> <span class="nx">console</span><span class="p">.</span><span class="nf">log</span><span class="p">(</span><span class="s2">`Count is: </span><span class="p">${</span><span class="nx">count</span><span class="p">}</span><span class="s2">`</span><span class="p">);</span> <span class="p">},</span> <span class="p">[</span><span class="nx">count</span><span class="p">]);</span> <span class="k">return </span><span class="p">(</span> <span class="p"><</span><span class="nt">div</span><span class="p">></span> <span class="p"><</span><span class="nt">button</span> <span class="na">onClick</span><span class="p">=</span><span class="si">{</span><span class="p">()</span> <span class="o">=></span> <span class="nf">setCount</span><span class="p">(</span><span class="nx">count</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="si">}</span><span class="p">></span>Increment<span class="p"></</span><span class="nt">button</span><span class="p">></span> <span class="p"><</span><span class="nt">p</span><span class="p">></span>Count: <span class="si">{</span><span class="nx">count</span><span class="si">}</span><span class="p"></</span><span class="nt">p</span><span class="p">></span> <span class="p"></</span><span class="nt">div</span><span class="p">></span> <span class="p">);</span> <span class="p">};</span> </code></pre> </div> <p>In this example, the <code>useState</code> hook is typed to ensure <code>count</code> is always a number, and <code>useEffect</code> logs the current count whenever it changes.</p> <p>By following these best practices, you’ll be able to harness the full potential of TypeScript in your React applications, leading to more robust, maintainable code.</p> <p>And before you go, if you’re also working with SQL, check out our <a href="https://github.com/Nierowheezy/sql-cheatsheet" rel="noopener noreferrer">Ultimate SQL Cheatsheet</a> to improve your SQL skills with both basic and advanced queries!</p></div>
<div><p>As a React developer, having the right tools can significantly improve your efficiency and workflow. Visual Studio Code (VS Code) is one of the most widely used code editors, but it’s the powerful extensions that truly elevate it. In this post, we’ll explore five essential VS Code extensions that will streamline your development process, help you write cleaner code, and enhance your overall productivity.</p> <h2> 1. ES7+ React/Redux/React-Native Snippets </h2> <p>This extension is a must-have for any React developer. It provides shortcuts for frequently used snippets in React and Redux. With just a few keystrokes, you can generate code for components, hooks, and Redux operations. It saves you from the repetitive task of typing out boilerplate code.</p> <p><strong>Key Features:</strong></p> <ul> <li>Shortcuts for functional components, imports, and more.</li> <li>Supports JavaScript and TypeScript.</li> <li>Snippets for React, Redux, React-Native, and GraphQL.</li> </ul> <p><strong>How It Helps:</strong> For example, typing <em>rafce</em> will generate a basic functional component structure:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight plaintext"><code>import React from 'react'; const ComponentName = () => { return <div></div>; }; export default ComponentName; </code></pre> </div> <p>This saves time, especially when working on large React projects with many components.</p> <p><strong>Get it here:</strong> <a href="https://marketplace.visualstudio.com/items?itemName=dsznajder.es7-react-js-snippets" rel="noopener noreferrer">ES7+ Snippets</a></p> <h2> 2. Prettier - Code Formatter </h2> <p>Prettier helps you write clean and consistent code by automatically formatting it according to predefined rules. This ensures that your codebase follows a consistent style, eliminating debates about formatting conventions and making collaboration easier.</p> <p><strong>Key Features:</strong></p> <ul> <li>Automatically formats code on save.</li> <li>Configurable to match your preferred style guide.</li> <li>Supports JavaScript, JSX, CSS, and other file types.</li> </ul> <p><strong>How It Helps:</strong> Prettier takes care of code styling, allowing you to focus on writing code without worrying about inconsistent formatting. It works seamlessly with other tools like ESLint to ensure both code correctness and style adherence.</p> <p><strong>Get it here:</strong> <a href="https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode" rel="noopener noreferrer">Prettier - Code Formatter</a></p> <h2> 3. ESLint </h2> <p>ESLint is an indispensable extension for React developers, helping you catch syntax errors and enforce coding standards. It works alongside Prettier to not only format your code but also highlight potential issues, ensuring your code is clean and error-free.</p> <p><strong>Key Features:</strong></p> <ul> <li>Catches common JavaScript and JSX errors.</li> <li>Configurable rules for team-wide consistency.</li> <li>Works with Prettier to ensure formatted and linted code.</li> </ul> <p><strong>How It Helps:</strong> ESLint can alert you to missing dependencies in <em>useEffect</em> hooks or unused variables, catching these issues early before they become larger bugs in production.</p> <p><strong>Get it here:</strong> <a href="https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint" rel="noopener noreferrer">ESLint</a></p> <h2> 4. Reactjs Code Snippets </h2> <p>Reactjs Code Snippets is a lightweight and convenient extension designed specifically for React developers. It provides shorthand snippets for frequently used React syntax, allowing you to quickly scaffold components, prop types, hooks, and more.</p> <p><strong>Key Features:</strong></p> <ul> <li>Quick snippets for functional components, lifecycle methods, and hooks.</li> <li>Autocompletes common React patterns, including JSX code.</li> <li>Supports both JavaScript and TypeScript.</li> </ul> <p><strong>How It Helps:</strong> Instead of manually typing out component structures or React hooks, you can use shorthand commands to auto-generate code snippets, speeding up the development process and minimizing repetitive tasks.</p> <p><strong>Get it here:</strong> <a href="https://marketplace.visualstudio.com/items?itemName=xabikos.ReactSnippets" rel="noopener noreferrer">Reactjs Code Snippets</a></p> <h2> 5. NPM Intellisense </h2> <p>Managing and importing packages efficiently is crucial when working with React projects. NPM Intellisense provides intelligent auto-completion for npm modules in your import statements, making it easy to import third-party libraries without needing to remember the exact names or paths.</p> <p><strong>Key Features:</strong></p> <ul> <li>Autocompletes npm module names in your import statements.</li> <li>Automatically updates and suggests installed packages.</li> <li>Speeds up importing by reducing the need to search for package names manually.</li> </ul> <p><strong>How It Helps:</strong> Instead of searching for package names or typing out long imports, NPM Intellisense helps you quickly add the correct module, increasing efficiency when managing dependencies in React projects.</p> <p><strong>Get it here:</strong> <a href="https://marketplace.visualstudio.com/items?itemName=christian-kohler.npm-intellisense" rel="noopener noreferrer">NPM Intellisense</a></p> <p>With these <strong>top 5 VS Code extensions</strong>, your React development experience will become more streamlined, productive, and less error-prone. From generating code snippets to improving formatting and automating imports, these tools make it easier to focus on building great applications.</p> <p>Do you have any favorite extensions that you can’t live without? Let me know in the comments below!</p></div>
<div><p>Data fetching is a core aspect of dynamic and responsive applications in modern web development. <a href="https://react.dev/" rel="noopener noreferrer">Reactjs</a> is a popular frontend library, which offers several ways to fetch and manage data.</p> <p>Whether you’re building a simple app or a complex large app, It’s important to understand the various methods you can use to fetch data in react</p> <p><em>While building a large and complex application, it is considered best practice to keep your files and folders clean and more organized way to avoid duplication and navigate easily and understandable. There are many ways but, I’m sharing with you the most popular way. Learn more about <a href="https://programmingly.dev/atomic-design-pattern-how-to-set-up-your-reactjs-project-structure/" rel="noopener noreferrer">Setting up your Reactjs project structure by using atomic design pattern</a>.</em></p> <p>In this article, I’m going to share 5 different methods for fetching data from servers using RESTful API in reactjs:</p> <ul> <li>Fetch API</li> <li>Axios</li> <li>React Query</li> <li>SWR</li> <li>GraphQL (Apollo Client)</li> </ul> <p>If you’re curious about native javascript ways, read this article on <a href="https://mroman7.medium.com/here-are-5-different-ways-to-make-api-calls-in-javascript-e6636ceba288" rel="noopener noreferrer">making API calls in javascript in 5 different ways</a>.</p> <p>1) Fetch API<br> The Fetch API is a modern & built-in javascript function for making asynchronous calls. It’s a native browser API that returns a promise. The Fetch API is built on top of javascript promises which makes handling requests much easier and cleaner to write.</p> <p>Here is an example of fetching data in reactjs using Fetch API:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight jsx"><code><span class="k">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">useEffect</span><span class="p">,</span> <span class="nx">useState</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">"</span><span class="s2">react</span><span class="dl">"</span><span class="p">;</span> <span class="k">export</span> <span class="k">default</span> <span class="kd">function</span> <span class="nf">PostsListWrapper</span><span class="p">()</span> <span class="p">{</span> <span class="kd">const</span> <span class="p">[</span><span class="nx">posts</span><span class="p">,</span> <span class="nx">setPosts</span><span class="p">]</span> <span class="o">=</span> <span class="nf">useState</span><span class="p">([]);</span> <span class="kd">const</span> <span class="p">[</span><span class="nx">loading</span><span class="p">,</span> <span class="nx">setLoading</span><span class="p">]</span> <span class="o">=</span> <span class="nf">useState</span><span class="p">(</span><span class="kc">true</span><span class="p">);</span> <span class="kd">const</span> <span class="nx">getPosts</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="k">return</span> <span class="nf">fetch</span><span class="p">(</span><span class="dl">"</span><span class="s2">https://jsonplaceholder.typicode.com/posts</span><span class="dl">"</span><span class="p">)</span> <span class="p">.</span><span class="nf">then</span><span class="p">((</span><span class="nx">response</span><span class="p">)</span> <span class="o">=></span> <span class="nx">response</span><span class="p">.</span><span class="nf">json</span><span class="p">())</span> <span class="p">.</span><span class="nf">then</span><span class="p">((</span><span class="nx">data</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span> <span class="nf">setPosts</span><span class="p">(</span><span class="nx">data</span><span class="p">);</span> <span class="nf">setLoading</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span> <span class="p">})</span> <span class="p">.</span><span class="k">catch</span><span class="p">((</span><span class="nx">error</span><span class="p">)</span> <span class="o">=></span> <span class="nx">console</span><span class="p">.</span><span class="nf">error</span><span class="p">(</span><span class="dl">"</span><span class="s2">Error: </span><span class="dl">"</span><span class="p">,</span> <span class="nx">error</span><span class="p">));</span> <span class="p">};</span> <span class="nf">useEffect</span><span class="p">(()</span> <span class="o">=></span> <span class="p">{</span> <span class="nf">getPosts</span><span class="p">();</span> <span class="p">},</span> <span class="p">[]);</span> <span class="k">if</span><span class="p">(</span><span class="nx">loading</span><span class="p">)</span> <span class="k">return</span> <span class="p"><</span><span class="nt">p</span><span class="p">></span>Loading...<span class="p"></</span><span class="nt">p</span><span class="p">></span> <span class="k">return </span><span class="p">(</span> <span class="p"><</span><span class="nt">section</span><span class="p">></span> <span class="p"><</span><span class="nt">h3</span><span class="p">></span>Posts List<span class="p"></</span><span class="nt">h3</span><span class="p">></span> <span class="p"><</span><span class="nt">ul</span><span class="p">></span><span class="si">{</span><span class="nx">posts</span> <span class="o">&&</span> <span class="nx">posts</span><span class="p">?.</span><span class="nf">map</span><span class="p">((</span><span class="nx">item</span><span class="p">,</span> <span class="nx">idx</span><span class="p">)</span> <span class="o">=></span> <span class="p"><</span><span class="nt">li</span> <span class="na">key</span><span class="p">=</span><span class="si">{</span><span class="nx">item</span><span class="p">.</span><span class="nx">id</span><span class="si">}</span><span class="p">></span><span class="si">{</span><span class="nx">item</span><span class="p">.</span><span class="nx">title</span><span class="si">}</span><span class="p"></</span><span class="nt">li</span><span class="p">>)</span><span class="si">}</span><span class="p"></</span><span class="nt">ul</span><span class="p">></span> <span class="p"></</span><span class="nt">section</span><span class="p">></span> <span class="p">);</span> <span class="p">}</span> </code></pre> </div> <p>2) Axios<br> <a href="https://axios-http.com/" rel="noopener noreferrer">Axios</a> is the most popular third-party library for making asynchronous calls, based on javascript promises. Axios is simpler, faster, and much more powerful than native Fetch API, it provides control over our HTTP request.</p> <p>Some of the best features I love that make it unique and powerful, such as,</p> <ul> <li>easy syntax</li> <li>creating different instances</li> <li>configure request</li> <li>playing with interceptors (middleware) to manipulate data in request and response</li> <li>retry or cancel the request</li> <li>handling errors</li> <li>and many others</li> </ul> <p>In order to use axios in the react application, you need to install it as a dependency. Run the below command to install Axios:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight shell"><code>npm i axios </code></pre> </div> <p>Let’s see Axios in action:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight jsx"><code><span class="k">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">useEffect</span><span class="p">,</span> <span class="nx">useState</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">"</span><span class="s2">react</span><span class="dl">"</span><span class="p">;</span> <span class="c1">// import axios </span> <span class="k">import</span> <span class="nx">axios</span> <span class="k">from</span> <span class="dl">"</span><span class="s2">axios</span><span class="dl">"</span><span class="p">;</span> <span class="c1">// defining a base url </span> <span class="kd">const</span> <span class="nx">BASE_URL</span> <span class="o">=</span> <span class="dl">"</span><span class="s2">https://jsonplaceholder.typicode.com</span><span class="dl">"</span><span class="p">;</span> <span class="c1">// creating an instance</span> <span class="kd">const</span> <span class="nx">publicInstance</span> <span class="o">=</span> <span class="nx">axios</span><span class="p">.</span><span class="nf">create</span><span class="p">({</span> <span class="na">baseURL</span><span class="p">:</span> <span class="nx">BASE_URL</span><span class="p">,</span> <span class="p">});</span> <span class="k">export</span> <span class="k">default</span> <span class="kd">function</span> <span class="nf">PostsListWrapper</span><span class="p">()</span> <span class="p">{</span> <span class="kd">const</span> <span class="p">[</span><span class="nx">posts</span><span class="p">,</span> <span class="nx">setPosts</span><span class="p">]</span> <span class="o">=</span> <span class="nf">useState</span><span class="p">([]);</span> <span class="kd">const</span> <span class="p">[</span><span class="nx">loading</span><span class="p">,</span> <span class="nx">setLoading</span><span class="p">]</span> <span class="o">=</span> <span class="nf">useState</span><span class="p">(</span><span class="kc">true</span><span class="p">);</span> <span class="kd">const</span> <span class="nx">getPosts</span> <span class="o">=</span> <span class="k">async </span><span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="k">try</span> <span class="p">{</span> <span class="kd">let</span> <span class="nx">posts</span> <span class="o">=</span> <span class="k">await</span> <span class="nf">publicInstance</span><span class="p">({</span> <span class="na">url</span><span class="p">:</span> <span class="dl">"</span><span class="s2">/posts</span><span class="dl">"</span><span class="p">,</span> <span class="na">method</span><span class="p">:</span> <span class="dl">"</span><span class="s2">GET</span><span class="dl">"</span><span class="p">,</span> <span class="p">});</span> <span class="nf">setLoading</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span> <span class="nf">setPosts</span><span class="p">(</span><span class="nx">posts</span><span class="p">?.</span><span class="nx">data</span><span class="p">);</span> <span class="p">}</span> <span class="k">catch </span><span class="p">(</span><span class="nx">error</span><span class="p">)</span> <span class="p">{</span> <span class="nx">console</span><span class="p">.</span><span class="nf">log</span><span class="p">(</span><span class="dl">"</span><span class="s2">Error: </span><span class="dl">"</span><span class="p">,</span> <span class="nx">error</span><span class="p">);</span> <span class="p">}</span> <span class="p">};</span> <span class="nf">useEffect</span><span class="p">(()</span> <span class="o">=></span> <span class="p">{</span> <span class="nf">getPosts</span><span class="p">();</span> <span class="p">},</span> <span class="p">[]);</span> <span class="k">if </span><span class="p">(</span><span class="nx">loading</span><span class="p">)</span> <span class="k">return</span> <span class="p"><</span><span class="nt">p</span><span class="p">></span>Loading...<span class="p"></</span><span class="nt">p</span><span class="p">>;</span> <span class="k">return </span><span class="p">(</span> <span class="p"><</span><span class="nt">section</span><span class="p">></span> <span class="p"><</span><span class="nt">h3</span><span class="p">></span>Posts List<span class="p"></</span><span class="nt">h3</span><span class="p">></span> <span class="p"><</span><span class="nt">ul</span><span class="p">></span> <span class="si">{</span><span class="nx">posts</span> <span class="o">&&</span> <span class="nx">posts</span><span class="p">?.</span><span class="nf">map</span><span class="p">((</span><span class="nx">item</span><span class="p">,</span> <span class="nx">idx</span><span class="p">)</span> <span class="o">=></span> <span class="p"><</span><span class="nt">li</span> <span class="na">key</span><span class="p">=</span><span class="si">{</span><span class="nx">item</span><span class="p">.</span><span class="nx">id</span><span class="si">}</span><span class="p">></span><span class="si">{</span><span class="nx">item</span><span class="p">.</span><span class="nx">title</span><span class="si">}</span><span class="p"></</span><span class="nt">li</span><span class="p">>)</span><span class="si">}</span> <span class="p"></</span><span class="nt">ul</span><span class="p">></span> <span class="p"></</span><span class="nt">section</span><span class="p">></span> <span class="p">);</span> <span class="p">}</span> </code></pre> </div> <p>3) React Query<br> React Query is a powerful data-fetching and state management library that handles caching, automatic prefetching, providing request status (loading, success, fetching, error, and more), and many other useful features that we have to manage manually. React Query library is built by TenStack, they build multiple useful and powerful react libraries like these to make work easier and faster. If you’re interested, take a quick start guide of react-query by following <a href="https://tanstack.com/query/latest/docs/framework/react/quick-start" rel="noopener noreferrer">this link</a>.</p> <p>You need to install the react-query as a third-party dependency to your react app to start using it. Use the below command to install react-query, and devtools to see requests in action.<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight shell"><code>npm i @tanstack/react-query @tanstack/react-query-devtools </code></pre> </div> <p>Here is an example of how to use React Query:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight jsx"><code><span class="k">import</span> <span class="nx">React</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="p">{</span> <span class="nx">useQuery</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react-query</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="nx">axios</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">axios</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">fetchPosts</span> <span class="o">=</span> <span class="k">async </span><span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="kd">const</span> <span class="p">{</span> <span class="nx">data</span> <span class="p">}</span> <span class="o">=</span> <span class="k">await</span> <span class="nx">axios</span><span class="p">.</span><span class="nf">get</span><span class="p">(</span><span class="dl">'</span><span class="s1">https://jsonplaceholder.typicode.com/posts</span><span class="dl">'</span><span class="p">);</span> <span class="k">return</span> <span class="nx">data</span><span class="p">;</span> <span class="p">};</span> <span class="k">export</span> <span class="k">default</span> <span class="kd">function</span> <span class="nf">PostsListWrapper</span><span class="p">()</span> <span class="p">{</span> <span class="kd">const</span> <span class="p">{</span> <span class="nx">data</span><span class="p">,</span> <span class="nx">error</span><span class="p">,</span> <span class="nx">isLoading</span><span class="p">,</span> <span class="nx">isSuccess</span> <span class="p">}</span> <span class="o">=</span> <span class="nf">useQuery</span><span class="p">(</span><span class="dl">'</span><span class="s1">posts</span><span class="dl">'</span><span class="p">,</span> <span class="nx">fetchPosts</span><span class="p">);</span> <span class="k">if </span><span class="p">(</span><span class="nx">isLoading</span><span class="p">)</span> <span class="k">return</span> <span class="p"><</span><span class="nt">p</span><span class="p">></span>Loading...<span class="p"></</span><span class="nt">p</span><span class="p">>;</span> <span class="k">if </span><span class="p">(</span><span class="nx">error</span><span class="p">)</span> <span class="k">return</span> <span class="p"><</span><span class="nt">p</span><span class="p">></span>Error fetching data.<span class="p"></</span><span class="nt">p</span><span class="p">>;</span> <span class="k">return </span><span class="p">(</span> <span class="p"><</span><span class="nt">div</span><span class="p">></span> <span class="p"><</span><span class="nt">h1</span><span class="p">></span>Posts<span class="p"></</span><span class="nt">h1</span><span class="p">></span> <span class="si">{</span><span class="nx">data</span><span class="p">.</span><span class="nf">map</span><span class="p">((</span><span class="nx">post</span><span class="p">)</span> <span class="o">=></span> <span class="p">(</span> <span class="p"><</span><span class="nt">div</span> <span class="na">key</span><span class="p">=</span><span class="si">{</span><span class="nx">post</span><span class="p">.</span><span class="nx">id</span><span class="si">}</span><span class="p">></span> <span class="p"><</span><span class="nt">h2</span><span class="p">></span><span class="si">{</span><span class="nx">post</span><span class="p">.</span><span class="nx">title</span><span class="si">}</span><span class="p"></</span><span class="nt">h2</span><span class="p">></span> <span class="p"><</span><span class="nt">p</span><span class="p">></span><span class="si">{</span><span class="nx">post</span><span class="p">.</span><span class="nx">body</span><span class="si">}</span><span class="p"></</span><span class="nt">p</span><span class="p">></span> <span class="p"></</span><span class="nt">div</span><span class="p">></span> <span class="p">))</span><span class="si">}</span> <span class="p"></</span><span class="nt">div</span><span class="p">></span> <span class="p">);</span> <span class="p">};</span> </code></pre> </div> <p>4) SWR<br> SWR (Stale While Revalidate) is a react hook-based library for making asynchronous calls. This library is created and maintained by Vercel (Company behind Nextjs) It focuses on cache management, revalidation, and minimal API requests.</p> <p><em>SWR is a strategy to first return the data from the cache (stale), then send the fetch request (revalidate), and finally come up with the up-to-date data. [by, <a href="https://swr.vercel.app/" rel="noopener noreferrer">SWR</a>]</em></p> <p>Like react-query, it caches data, revalidates provides request status (loading, success, error). The reason it is recommended is because it is:</p> <ul> <li>lightweight</li> <li>easy syntax</li> <li>fast</li> <li>provides real-time experience</li> <li>and SSR / ISR / SSG ready</li> </ul> <p>You can install SWR using the below command:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight shell"><code>npm i swr </code></pre> </div> <p>Let’s understand it with an example in action:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight plaintext"><code>import React from 'react'; import useSWR from 'swr'; const fetcher = (url) => fetch(url).then((res) => res.json()); export default function PostsListWrapper() { const { data, error, isLoading } = useSWR('https://jsonplaceholder.typicode.com/posts', fetcher); if (isLoading) return <p>Loading...</p>; if (error) return <p>Error fetching data.</p>; return ( <div> <h1>Posts</h1> {data.map((post) => ( <div key={post.id}> <h2>{post.title}</h2> <p>{post.body}</p> </div> ))} </div> ); }; </code></pre> </div> <p>5) GraphQL with Apollo Client<br> GraphQL is becoming a new standard for making API calls. It provides efficient data handling and state management.</p> <p>GraphQL on its own is a big topic, so I’ll try to keep it simple and short. GraphQL is like a query language for APIs, it means instead of RESTful API which provides all the field values and keys, GraphQL allows the frontend to request exactly the data they need in a single request. This reduces the over-fetching and unnecessary data.</p> <p>Apollo Client on the other hand is a state management library that integrates with GraphQL. It is like a wrapper around GraphQL in our app which simplifies the process of fetching, caching, and managing data in client applications. It works with many different frameworks like React, Vue, Angular, and others. <a href="https://www.apollographql.com/docs/react/#community-integrations" rel="noopener noreferrer">See the Full list of supported frameworks</a>.</p> <p>In order to use it in your react app, install the below command:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight shell"><code>npm <span class="nb">install</span> @apollo/client graphql </code></pre> </div> <p>See Apollo GraphQL in action:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight jsx"><code><span class="k">import</span> <span class="nx">React</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="p">{</span> <span class="nx">useQuery</span><span class="p">,</span> <span class="nx">gql</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">@apollo/client</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">GET_POSTS</span> <span class="o">=</span> <span class="nx">gql</span><span class="s2">` query GetPosts { posts { id title body } } `</span><span class="p">;</span> <span class="k">export</span> <span class="k">default</span> <span class="kd">function</span> <span class="nf">PostsListWrapper</span><span class="p">()</span> <span class="p">{</span> <span class="kd">const</span> <span class="p">{</span> <span class="nx">loading</span><span class="p">,</span> <span class="nx">error</span><span class="p">,</span> <span class="nx">data</span> <span class="p">}</span> <span class="o">=</span> <span class="nf">useQuery</span><span class="p">(</span><span class="nx">GET_POSTS</span><span class="p">);</span> <span class="k">if </span><span class="p">(</span><span class="nx">loading</span><span class="p">)</span> <span class="k">return</span> <span class="p"><</span><span class="nt">p</span><span class="p">></span>Loading...<span class="p"></</span><span class="nt">p</span><span class="p">>;</span> <span class="k">if </span><span class="p">(</span><span class="nx">error</span><span class="p">)</span> <span class="k">return</span> <span class="p"><</span><span class="nt">p</span><span class="p">></span>Error fetching data<span class="p"></</span><span class="nt">p</span><span class="p">>;</span> <span class="k">return </span><span class="p">(</span> <span class="p"><</span><span class="nt">div</span><span class="p">></span> <span class="p"><</span><span class="nt">h1</span><span class="p">></span>Posts<span class="p"></</span><span class="nt">h1</span><span class="p">></span> <span class="si">{</span><span class="nx">data</span><span class="p">.</span><span class="nx">posts</span><span class="p">.</span><span class="nf">map</span><span class="p">((</span><span class="nx">post</span><span class="p">)</span> <span class="o">=></span> <span class="p">(</span> <span class="p"><</span><span class="nt">div</span> <span class="na">key</span><span class="p">=</span><span class="si">{</span><span class="nx">post</span><span class="p">.</span><span class="nx">id</span><span class="si">}</span><span class="p">></span> <span class="p"><</span><span class="nt">h2</span><span class="p">></span><span class="si">{</span><span class="nx">post</span><span class="p">.</span><span class="nx">title</span><span class="si">}</span><span class="p"></</span><span class="nt">h2</span><span class="p">></span> <span class="p"><</span><span class="nt">p</span><span class="p">></span><span class="si">{</span><span class="nx">post</span><span class="p">.</span><span class="nx">body</span><span class="si">}</span><span class="p"></</span><span class="nt">p</span><span class="p">></span> <span class="p"></</span><span class="nt">div</span><span class="p">></span> <span class="p">))</span><span class="si">}</span> <span class="p"></</span><span class="nt">div</span><span class="p">></span> <span class="p">);</span> <span class="p">};</span> </code></pre> </div> <p>This blog is originally posted at <a href="https://programmingly.dev/" rel="noopener noreferrer">Programmingly.dev</a>. Read this full article at: <a href="https://programmingly.dev/here-are-5-different-ways-to-make-api-call-in-reactjs/" rel="noopener noreferrer">Here are 5 different ways to make API call in reactjs</a></p></div>
<div><h4> Introduction </h4> <p>Styling is a crucial aspect of web development that ensures your applications are visually appealing and user-friendly. React offers several approaches to styling components, from traditional CSS and Sass to modern CSS-in-JS solutions like Styled-Components. This week, we'll dive into these methods and learn how to apply them effectively in your React projects.</p> <h4> Importance of Styling in React </h4> <p>Proper styling enhances the user experience, improves usability, and makes your application more engaging. Understanding different styling techniques allows you to choose the best approach for your specific project needs.</p> <h4> Traditional CSS </h4> <p><strong>Using CSS with React:</strong></p> <ul> <li> <strong>Basic Example</strong>: </li> </ul> <div class="highlight js-code-highlight"> <pre class="highlight javascript"><code> <span class="k">import</span> <span class="nx">React</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="dl">'</span><span class="s1">./App.css</span><span class="dl">'</span><span class="p">;</span> <span class="kd">function</span> <span class="nf">App</span><span class="p">()</span> <span class="p">{</span> <span class="k">return </span><span class="p">(</span> <span class="o"><</span><span class="nx">div</span> <span class="nx">className</span><span class="o">=</span><span class="dl">"</span><span class="s2">container</span><span class="dl">"</span><span class="o">></span> <span class="o"><</span><span class="nx">h1</span> <span class="nx">className</span><span class="o">=</span><span class="dl">"</span><span class="s2">title</span><span class="dl">"</span><span class="o">></span><span class="nx">Hello</span><span class="p">,</span> <span class="nx">World</span><span class="o">!<</span><span class="sr">/h1</span><span class="err">> </span> <span class="o"><</span><span class="sr">/div</span><span class="err">> </span> <span class="p">);</span> <span class="p">}</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">App</span><span class="p">;</span> </code></pre> </div> <ul> <li> <strong>App.css</strong>: </li> </ul> <div class="highlight js-code-highlight"> <pre class="highlight css"><code> <span class="nc">.container</span> <span class="p">{</span> <span class="nl">text-align</span><span class="p">:</span> <span class="nb">center</span><span class="p">;</span> <span class="nl">padding</span><span class="p">:</span> <span class="m">20px</span><span class="p">;</span> <span class="p">}</span> <span class="nc">.title</span> <span class="p">{</span> <span class="nl">color</span><span class="p">:</span> <span class="no">blue</span><span class="p">;</span> <span class="nl">font-size</span><span class="p">:</span> <span class="m">2em</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <p><strong>CSS Modules:</strong></p> <ul> <li> <strong>Example</strong>: </li> </ul> <div class="highlight js-code-highlight"> <pre class="highlight javascript"><code> <span class="k">import</span> <span class="nx">React</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="nx">styles</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">./App.module.css</span><span class="dl">'</span><span class="p">;</span> <span class="kd">function</span> <span class="nf">App</span><span class="p">()</span> <span class="p">{</span> <span class="k">return </span><span class="p">(</span> <span class="o"><</span><span class="nx">div</span> <span class="nx">className</span><span class="o">=</span><span class="p">{</span><span class="nx">styles</span><span class="p">.</span><span class="nx">container</span><span class="p">}</span><span class="o">></span> <span class="o"><</span><span class="nx">h1</span> <span class="nx">className</span><span class="o">=</span><span class="p">{</span><span class="nx">styles</span><span class="p">.</span><span class="nx">title</span><span class="p">}</span><span class="o">></span><span class="nx">Hello</span><span class="p">,</span> <span class="nx">World</span><span class="o">!<</span><span class="sr">/h1</span><span class="err">> </span> <span class="o"><</span><span class="sr">/div</span><span class="err">> </span> <span class="p">);</span> <span class="p">}</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">App</span><span class="p">;</span> </code></pre> </div> <ul> <li> <strong>App.module.css</strong>: </li> </ul> <div class="highlight js-code-highlight"> <pre class="highlight css"><code> <span class="nc">.container</span> <span class="p">{</span> <span class="nl">text-align</span><span class="p">:</span> <span class="nb">center</span><span class="p">;</span> <span class="nl">padding</span><span class="p">:</span> <span class="m">20px</span><span class="p">;</span> <span class="p">}</span> <span class="nc">.title</span> <span class="p">{</span> <span class="nl">color</span><span class="p">:</span> <span class="no">blue</span><span class="p">;</span> <span class="nl">font-size</span><span class="p">:</span> <span class="m">2em</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <h4> Using Sass </h4> <p><strong>Installing Sass:</strong></p> <ul> <li> <strong>Command to Install</strong>: </li> </ul> <div class="highlight js-code-highlight"> <pre class="highlight shell"><code> npm <span class="nb">install </span>node-sass </code></pre> </div> <p><strong>Using Sass in React:</strong></p> <ul> <li> <strong>App.scss</strong>: </li> </ul> <div class="highlight js-code-highlight"> <pre class="highlight scss"><code> <span class="nv">$primary-color</span><span class="p">:</span> <span class="no">blue</span><span class="p">;</span> <span class="nv">$padding</span><span class="p">:</span> <span class="m">20px</span><span class="p">;</span> <span class="nc">.container</span> <span class="p">{</span> <span class="nl">text-align</span><span class="p">:</span> <span class="nb">center</span><span class="p">;</span> <span class="nl">padding</span><span class="p">:</span> <span class="nv">$padding</span><span class="p">;</span> <span class="p">}</span> <span class="nc">.title</span> <span class="p">{</span> <span class="nl">color</span><span class="p">:</span> <span class="nv">$primary-color</span><span class="p">;</span> <span class="nl">font-size</span><span class="p">:</span> <span class="m">2em</span><span class="p">;</span> <span class="p">}</span> </code></pre> </div> <ul> <li> <strong>App Component</strong>: </li> </ul> <div class="highlight js-code-highlight"> <pre class="highlight javascript"><code> <span class="k">import</span> <span class="nx">React</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="dl">'</span><span class="s1">./App.scss</span><span class="dl">'</span><span class="p">;</span> <span class="kd">function</span> <span class="nf">App</span><span class="p">()</span> <span class="p">{</span> <span class="k">return </span><span class="p">(</span> <span class="o"><</span><span class="nx">div</span> <span class="nx">className</span><span class="o">=</span><span class="dl">"</span><span class="s2">container</span><span class="dl">"</span><span class="o">></span> <span class="o"><</span><span class="nx">h1</span> <span class="nx">className</span><span class="o">=</span><span class="dl">"</span><span class="s2">title</span><span class="dl">"</span><span class="o">></span><span class="nx">Hello</span><span class="p">,</span> <span class="nx">World</span><span class="o">!<</span><span class="sr">/h1</span><span class="err">> </span> <span class="o"><</span><span class="sr">/div</span><span class="err">> </span> <span class="p">);</span> <span class="p">}</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">App</span><span class="p">;</span> </code></pre> </div> <p><strong>Nested Styling with Sass:</strong></p> <ul> <li> <strong>Example</strong>: </li> </ul> <div class="highlight js-code-highlight"> <pre class="highlight scss"><code> <span class="nc">.container</span> <span class="p">{</span> <span class="nl">text-align</span><span class="p">:</span> <span class="nb">center</span><span class="p">;</span> <span class="nl">padding</span><span class="p">:</span> <span class="m">20px</span><span class="p">;</span> <span class="nc">.title</span> <span class="p">{</span> <span class="nl">color</span><span class="p">:</span> <span class="no">blue</span><span class="p">;</span> <span class="nl">font-size</span><span class="p">:</span> <span class="m">2em</span><span class="p">;</span> <span class="k">&</span><span class="nd">:hover</span> <span class="p">{</span> <span class="nl">color</span><span class="p">:</span> <span class="no">darkblue</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> </code></pre> </div> <h4> Styled-Components </h4> <p><strong>Introduction to Styled-Components:</strong></p> <ul> <li> <strong>Definition</strong>: A library for styling React components using tagged template literals.</li> <li> <strong>Installation</strong>: </li> </ul> <div class="highlight js-code-highlight"> <pre class="highlight shell"><code> npm <span class="nb">install </span>styled-components </code></pre> </div> <p><strong>Using Styled-Components:</strong></p> <ul> <li> <strong>Example</strong>: </li> </ul> <div class="highlight js-code-highlight"> <pre class="highlight javascript"><code> <span class="k">import</span> <span class="nx">React</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="nx">styled</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">styled-components</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">Container</span> <span class="o">=</span> <span class="nx">styled</span><span class="p">.</span><span class="nx">div</span><span class="s2">` text-align: center; padding: 20px; `</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">Title</span> <span class="o">=</span> <span class="nx">styled</span><span class="p">.</span><span class="nx">h1</span><span class="s2">` color: blue; font-size: 2em; &:hover { color: darkblue; } `</span><span class="p">;</span> <span class="kd">function</span> <span class="nf">App</span><span class="p">()</span> <span class="p">{</span> <span class="k">return </span><span class="p">(</span> <span class="o"><</span><span class="nx">Container</span><span class="o">></span> <span class="o"><</span><span class="nx">Title</span><span class="o">></span><span class="nx">Hello</span><span class="p">,</span> <span class="nx">World</span><span class="o">!<</span><span class="sr">/Title</span><span class="err">> </span> <span class="o"><</span><span class="sr">/Container</span><span class="err">> </span> <span class="p">);</span> <span class="p">}</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">App</span><span class="p">;</span> </code></pre> </div> <p><strong>Theming with Styled-Components:</strong></p> <ul> <li> <strong>Creating a Theme</strong>: </li> </ul> <div class="highlight js-code-highlight"> <pre class="highlight javascript"><code> <span class="k">import</span> <span class="p">{</span> <span class="nx">ThemeProvider</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">styled-components</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">theme</span> <span class="o">=</span> <span class="p">{</span> <span class="na">colors</span><span class="p">:</span> <span class="p">{</span> <span class="na">primary</span><span class="p">:</span> <span class="dl">'</span><span class="s1">blue</span><span class="dl">'</span><span class="p">,</span> <span class="na">secondary</span><span class="p">:</span> <span class="dl">'</span><span class="s1">darkblue</span><span class="dl">'</span> <span class="p">},</span> <span class="na">spacing</span><span class="p">:</span> <span class="p">{</span> <span class="na">padding</span><span class="p">:</span> <span class="dl">'</span><span class="s1">20px</span><span class="dl">'</span> <span class="p">}</span> <span class="p">};</span> <span class="kd">function</span> <span class="nf">App</span><span class="p">()</span> <span class="p">{</span> <span class="k">return </span><span class="p">(</span> <span class="o"><</span><span class="nx">ThemeProvider</span> <span class="nx">theme</span><span class="o">=</span><span class="p">{</span><span class="nx">theme</span><span class="p">}</span><span class="o">></span> <span class="o"><</span><span class="nx">Container</span><span class="o">></span> <span class="o"><</span><span class="nx">Title</span><span class="o">></span><span class="nx">Hello</span><span class="p">,</span> <span class="nx">World</span><span class="o">!<</span><span class="sr">/Title</span><span class="err">> </span> <span class="o"><</span><span class="sr">/Container</span><span class="err">> </span> <span class="o"><</span><span class="sr">/ThemeProvider</span><span class="err">> </span> <span class="p">);</span> <span class="p">}</span> </code></pre> </div> <ul> <li> <strong>Using Theme Values</strong>: </li> </ul> <div class="highlight js-code-highlight"> <pre class="highlight javascript"><code> <span class="k">import</span> <span class="nx">styled</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">styled-components</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">Container</span> <span class="o">=</span> <span class="nx">styled</span><span class="p">.</span><span class="nx">div</span><span class="s2">` text-align: center; padding: </span><span class="p">${(</span><span class="nx">props</span><span class="p">)</span> <span class="o">=></span> <span class="nx">props</span><span class="p">.</span><span class="nx">theme</span><span class="p">.</span><span class="nx">spacing</span><span class="p">.</span><span class="nx">padding</span><span class="p">}</span><span class="s2">; `</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">Title</span> <span class="o">=</span> <span class="nx">styled</span><span class="p">.</span><span class="nx">h1</span><span class="s2">` color: </span><span class="p">${(</span><span class="nx">props</span><span class="p">)</span> <span class="o">=></span> <span class="nx">props</span><span class="p">.</span><span class="nx">theme</span><span class="p">.</span><span class="nx">colors</span><span class="p">.</span><span class="nx">primary</span><span class="p">}</span><span class="s2">; font-size: 2em; &:hover { color: </span><span class="p">${(</span><span class="nx">props</span><span class="p">)</span> <span class="o">=></span> <span class="nx">props</span><span class="p">.</span><span class="nx">theme</span><span class="p">.</span><span class="nx">colors</span><span class="p">.</span><span class="nx">secondary</span><span class="p">}</span><span class="s2">; } `</span><span class="p">;</span> </code></pre> </div> <h4> Conclusion </h4> <p>Choosing the right styling approach in React depends on your project requirements and personal preference. Traditional CSS and Sass offer familiarity and simplicity, while Styled-Components provide dynamic and scoped styling capabilities. Mastering these techniques will help you build beautiful and maintainable user interfaces.</p> <h4> Resources for Further Learning </h4> <ul> <li> <strong>Online Courses</strong>: Websites like Udemy, Pluralsight, and freeCodeCamp offer courses on React styling techniques.</li> <li> <strong>Books</strong>: "React and React Native" by Adam Boduch and "React Quickly" by Azat Mardan.</li> <li> <strong>Documentation and References</strong>: <ul> <li><a href="https://styled-components.com/docs">Styled-Components Documentation</a></li> <li><a href="https://sass-lang.com/documentation">Sass Documentation</a></li> <li><a href="https://github.com/css-modules/css-modules">React CSS Modules Documentation</a></li> </ul> </li> <li> <strong>Communities</strong>: Join developer communities on platforms like Stack Overflow, Reddit, and GitHub for support and networking.</li> </ul></div>
Introduction What is bdd ? Behavior Driven Development (BDD) is an agile software development methodology that emphasizes collaboration among all stakeholders—developers, testers, and business analysts—to create a shared understanding of the behavior of a software application. BDD extends Test Driven Development (TDD) by focusing on the behavior of the application from the end user's perspective. It uses natural language constructs to describe the expected behavior, making it accessible to non-
<div><p>UI libraries that can create aesthetically pleasing websites are what every developer looks for. These libraries not only help developers but also make life easier for users by enabling them to navigate the system quickly. So, what are some great UI libraries that developers can use with React.js? Let’s see them one by one.</p> <p>Before that, it’s imperative to understand what React UI component libraries are. These libraries are *<em>collections of pre-coded user-interface elements. *</em> The elements included in component libraries are buttons, cards, tables, etc. Ultimately, they help developers ensure quick development and prototyping.</p> <p>Now, let’s see some of the best React UI Component Libraries.</p> <p><strong>Chakra UI</strong></p> <p>It’s one of the simpler UI libraries loved by almost every developer because of its modular and accessible components. It is built on top of React UI primitives. Moreover, to ensure that all components are accessible, Chakra UI uses WAI-ARIA guidelines. Some pros of using Chakra UI include:</p> <ul> <li>Responsive components</li> <li>Availability of stack components</li> <li>Dark mode</li> <li>Multiple color modes</li> <li>Custom values</li> </ul> <p>But why should you use it over others? Chakra UI's main focus is accessibility. This ensures that the app you build is usable by everyone. Additionally, it emphasizes simplicity. This might also be why it’s popular. As of today, Chakra UI has more than 37k stars on GitHub.</p> <p><strong>Prime React</strong></p> <p>Another library that developers love to use is PrimeReact. It is an open-source front-end library that helps in building responsive websites. However, for beginners, it’s challenging to learn. Some also call it the underdog because it’s not very popular. Part of the reason might be marketing. On GitHub, it has 6.1k stars. Why do I love this library? Developers don’t need to install extra packages to accomplish tasks. They can do a lot with just the library.</p> <p>Some pros of using PrimeReact include:</p> <ul> <li>Data table components</li> <li>Customizable templates and themes</li> <li>Responsiveness</li> <li>Better accessibility</li> </ul> <p><strong>NextUI</strong></p> <p>Another library that is popular in React.js is NextUI. It is renowned for its flexible nature and performance. NextUI is particularly recommended for building landing pages quickly. Moreover, NextUI offers immediate styling and components that developers can use after making some edits. It’s built on top of TailWind CSS and React Aria. Some advantages of NextUI include:</p> <ul> <li>Gentle learning curve</li> <li>Use of high-quality and customizable components</li> <li>Adherence to WAI-ARIA guidelines</li> <li>Well-scaled default dark theme</li> <li>Lazy loading (helpful for app performance)</li> <li>Support for internationalization</li> <li>Form validation</li> </ul> <p><strong>Semantic UI</strong></p> <p>If you prefer a library that is full of pre-built components then you need to use semantic UI. Some of the advantages of using Semantic UI include:</p> <ul> <li>Auto-controlled state</li> <li>Shorthand props</li> <li>Augmentation</li> <li>Customization of themes</li> <li>Community support</li> </ul> <p><strong>Ant Design</strong></p> <p>If you want to build enterprise applications, then go for Ant Design. This is because this UI library has many features that can make applications accessible as well as scalable. In fact, the reason it is too popular is because it has good performance and accessibility. Moreover, it gives developers the ability to tailor design elements as well. There are varying internalisation tools, too. The best part about Ant Design is that it enables the creation of a minimalist website. Ant Design focuses more on simplicity and consistency. </p> <ul> <li>Typescript friendly</li> <li>Comprehensive</li> <li>Best (if you have to take a lot of input)</li> <li>Variety and customization</li> <li>Thorough documentation</li> </ul></div>
## Introduction As a Software Development Engineer (SDE), your role involves not only writing code but also managing projects, collaborating with teams, and continuously learning new technologies. To succeed and excel in this fast-paced field, it’s essential to leverage the right tools and adopt effective practices. Here are some tips and tools that can help you become a more efficient and productive SDE. Tips for SDEs 1. Master Version Control Version control is a fundam
Shaping Today’s Digital Landscape Coding has surged in popularity worldwide due to its pivotal role in technology’s evolution ...
We are always told that efficiency is key, and in 2024, it cannot be truer with such high competition across industries. Thankfully, the beginning of the artificial intelligence (AI) powered tools revolution has led the way for tools and software that enormously reduce the time required and boost productivity to complete different tasks, making it possible to achieve a day’s work in just one hour, helping you save time. There is an AI tool for everyone, whether for music production, creating new
Introducing Easy UI ✨ - High quality templates for web designers. I am building 50+ free website templates using React, Next.js, TypeScript, Tailwind Labs CSS, Shadcn-UI, and Framer Motion. Today, I am super excited to launch first 3 templates, 1.Easy Template - Template suitable for SaaS products 2.Designfast - Minimal template designed for service/creative business 3.QuotesAI - Ready-to-use Micro SaaS with NextAuth built-in Here's the link to checkout Easy UI
The Great Resignation and the global shift to remote work has opened up the job market. But today's market conditions have swung the power back into the hands of HR. Layoffs, hiring freezes, ghosting and greater competition make securing a new job a harder, longer progress. Applying for a job is arduous and time-consuming, and it's tempting to use the fast apply button and attach a generic cover letter to every application -- but that's not going to get you hired in this mark
<div><p>Certainly, here are some examples related to React and Redux:</p> <ol> <li> <strong>React Example:</strong> </li> </ol> <div class="highlight js-code-highlight"> <pre class="highlight jsx"><code> <span class="k">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">useState</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">Counter</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="kd">const</span> <span class="p">[</span><span class="nx">count</span><span class="p">,</span> <span class="nx">setCount</span><span class="p">]</span> <span class="o">=</span> <span class="nf">useState</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span> <span class="kd">const</span> <span class="nx">increment</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="nf">setCount</span><span class="p">(</span><span class="nx">count</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span> <span class="kd">const</span> <span class="nx">decrement</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="nf">setCount</span><span class="p">(</span><span class="nx">count</span> <span class="o">-</span> <span class="mi">1</span><span class="p">);</span> <span class="k">return </span><span class="p">(</span> <span class="p"><</span><span class="nt">div</span><span class="p">></span> <span class="p"><</span><span class="nt">p</span><span class="p">></span>Count: <span class="si">{</span><span class="nx">count</span><span class="si">}</span><span class="p"></</span><span class="nt">p</span><span class="p">></span> <span class="p"><</span><span class="nt">button</span> <span class="na">onClick</span><span class="p">=</span><span class="si">{</span><span class="nx">increment</span><span class="si">}</span><span class="p">></span>Increment<span class="p"></</span><span class="nt">button</span><span class="p">></span> <span class="p"><</span><span class="nt">button</span> <span class="na">onClick</span><span class="p">=</span><span class="si">{</span><span class="nx">decrement</span><span class="si">}</span><span class="p">></span>Decrement<span class="p"></</span><span class="nt">button</span><span class="p">></span> <span class="p"></</span><span class="nt">div</span><span class="p">></span> <span class="p">);</span> <span class="p">};</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">Counter</span><span class="p">;</span> </code></pre> </div> <ol> <li> <p><strong>Redux Example:</strong></p> <ul> <li>Actions: </li> </ul> <pre class="highlight javascript"><code> <span class="c1">// action types</span> <span class="kd">const</span> <span class="nx">INCREMENT</span> <span class="o">=</span> <span class="dl">'</span><span class="s1">INCREMENT</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">DECREMENT</span> <span class="o">=</span> <span class="dl">'</span><span class="s1">DECREMENT</span><span class="dl">'</span><span class="p">;</span> <span class="c1">// action creators</span> <span class="kd">const</span> <span class="nx">increment</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">({</span> <span class="na">type</span><span class="p">:</span> <span class="nx">INCREMENT</span> <span class="p">});</span> <span class="kd">const</span> <span class="nx">decrement</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">({</span> <span class="na">type</span><span class="p">:</span> <span class="nx">DECREMENT</span> <span class="p">});</span> </code></pre> </li> </ol> <ul> <li> <p>Reducer:<br> </p> <pre class="highlight javascript"><code> <span class="kd">const</span> <span class="nx">counterReducer</span> <span class="o">=</span> <span class="p">(</span><span class="nx">state</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">action</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span> <span class="k">switch </span><span class="p">(</span><span class="nx">action</span><span class="p">.</span><span class="nx">type</span><span class="p">)</span> <span class="p">{</span> <span class="k">case</span> <span class="na">INCREMENT</span><span class="p">:</span> <span class="k">return</span> <span class="nx">state</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="k">case</span> <span class="na">DECREMENT</span><span class="p">:</span> <span class="k">return</span> <span class="nx">state</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="nl">default</span><span class="p">:</span> <span class="k">return</span> <span class="nx">state</span><span class="p">;</span> <span class="p">}</span> <span class="p">};</span> </code></pre> </li> <li> <p>Store:<br> </p> <pre class="highlight javascript"><code> <span class="k">import</span> <span class="p">{</span> <span class="nx">createStore</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">redux</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">store</span> <span class="o">=</span> <span class="nf">createStore</span><span class="p">(</span><span class="nx">counterReducer</span><span class="p">);</span> </code></pre> </li> <li> <p>Connecting React Component:<br> </p> <pre class="highlight jsx"><code> <span class="k">import</span> <span class="nx">React</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="p">{</span> <span class="nx">useSelector</span><span class="p">,</span> <span class="nx">useDispatch</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react-redux</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="p">{</span> <span class="nx">increment</span><span class="p">,</span> <span class="nx">decrement</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">./actions</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">Counter</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="kd">const</span> <span class="nx">count</span> <span class="o">=</span> <span class="nf">useSelector</span><span class="p">((</span><span class="nx">state</span><span class="p">)</span> <span class="o">=></span> <span class="nx">state</span><span class="p">);</span> <span class="kd">const</span> <span class="nx">dispatch</span> <span class="o">=</span> <span class="nf">useDispatch</span><span class="p">();</span> <span class="k">return </span><span class="p">(</span> <span class="p"><</span><span class="nt">div</span><span class="p">></span> <span class="p"><</span><span class="nt">p</span><span class="p">></span>Count: <span class="si">{</span><span class="nx">count</span><span class="si">}</span><span class="p"></</span><span class="nt">p</span><span class="p">></span> <span class="p"><</span><span class="nt">button</span> <span class="na">onClick</span><span class="p">=</span><span class="si">{</span><span class="p">()</span> <span class="o">=></span> <span class="nf">dispatch</span><span class="p">(</span><span class="nf">increment</span><span class="p">())</span><span class="si">}</span><span class="p">></span>Increment<span class="p"></</span><span class="nt">button</span><span class="p">></span> <span class="p"><</span><span class="nt">button</span> <span class="na">onClick</span><span class="p">=</span><span class="si">{</span><span class="p">()</span> <span class="o">=></span> <span class="nf">dispatch</span><span class="p">(</span><span class="nf">decrement</span><span class="p">())</span><span class="si">}</span><span class="p">></span>Decrement<span class="p"></</span><span class="nt">button</span><span class="p">></span> <span class="p"></</span><span class="nt">div</span><span class="p">></span> <span class="p">);</span> <span class="p">};</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">Counter</span><span class="p">;</span> </code></pre> </li> </ul> <p>These examples cover a basic React component and a simple Redux setup with actions, a reducer, and a connected React component.</p> <p>Certainly, let's delve into more topics with additional example code:</p> <ol> <li> <p><strong>React Router Example:</strong></p> <ul> <li>Installation: </li> </ul> <pre class="highlight shell"><code> npm <span class="nb">install </span>react-router-dom </code></pre> </li> </ol> <ul> <li> <p>Usage:<br> </p> <pre class="highlight jsx"><code> <span class="k">import</span> <span class="nx">React</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="p">{</span> <span class="nx">BrowserRouter</span> <span class="k">as</span> <span class="nx">Router</span><span class="p">,</span> <span class="nx">Route</span><span class="p">,</span> <span class="nx">Link</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react-router-dom</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">Home</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p"><</span><span class="nt">h2</span><span class="p">></span>Home<span class="p"></</span><span class="nt">h2</span><span class="p">>;</span> <span class="kd">const</span> <span class="nx">About</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p"><</span><span class="nt">h2</span><span class="p">></span>About<span class="p"></</span><span class="nt">h2</span><span class="p">>;</span> <span class="kd">const</span> <span class="nx">App</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">(</span> <span class="p"><</span><span class="nc">Router</span><span class="p">></span> <span class="p"><</span><span class="nt">nav</span><span class="p">></span> <span class="p"><</span><span class="nc">Link</span> <span class="na">to</span><span class="p">=</span><span class="s">"/"</span><span class="p">></span>Home<span class="p"></</span><span class="nc">Link</span><span class="p">></span> <span class="p"><</span><span class="nc">Link</span> <span class="na">to</span><span class="p">=</span><span class="s">"/about"</span><span class="p">></span>About<span class="p"></</span><span class="nc">Link</span><span class="p">></span> <span class="p"></</span><span class="nt">nav</span><span class="p">></span> <span class="p"><</span><span class="nc">Route</span> <span class="na">path</span><span class="p">=</span><span class="s">"/"</span> <span class="na">exact</span> <span class="na">component</span><span class="p">=</span><span class="si">{</span><span class="nx">Home</span><span class="si">}</span> <span class="p">/></span> <span class="p"><</span><span class="nc">Route</span> <span class="na">path</span><span class="p">=</span><span class="s">"/about"</span> <span class="na">component</span><span class="p">=</span><span class="si">{</span><span class="nx">About</span><span class="si">}</span> <span class="p">/></span> <span class="p"></</span><span class="nc">Router</span><span class="p">></span> <span class="p">);</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">App</span><span class="p">;</span> </code></pre> </li> </ul> <ol> <li> <p><strong>Redux Thunk Example:</strong></p> <ul> <li>Installation: </li> </ul> <pre class="highlight shell"><code> npm <span class="nb">install </span>redux-thunk </code></pre> </li> </ol> <ul> <li> <p>Usage (Action with Thunk):<br> </p> <pre class="highlight javascript"><code> <span class="c1">// Action Creator using Thunk</span> <span class="kd">const</span> <span class="nx">fetchData</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="k">return </span><span class="p">(</span><span class="nx">dispatch</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span> <span class="nf">dispatch</span><span class="p">({</span> <span class="na">type</span><span class="p">:</span> <span class="dl">'</span><span class="s1">FETCH_DATA_REQUEST</span><span class="dl">'</span> <span class="p">});</span> <span class="c1">// Perform asynchronous operation (e.g., API call)</span> <span class="nf">fetch</span><span class="p">(</span><span class="dl">'</span><span class="s1">https://api.example.com/data</span><span class="dl">'</span><span class="p">)</span> <span class="p">.</span><span class="nf">then</span><span class="p">((</span><span class="nx">response</span><span class="p">)</span> <span class="o">=></span> <span class="nx">response</span><span class="p">.</span><span class="nf">json</span><span class="p">())</span> <span class="p">.</span><span class="nf">then</span><span class="p">((</span><span class="nx">data</span><span class="p">)</span> <span class="o">=></span> <span class="nf">dispatch</span><span class="p">({</span> <span class="na">type</span><span class="p">:</span> <span class="dl">'</span><span class="s1">FETCH_DATA_SUCCESS</span><span class="dl">'</span><span class="p">,</span> <span class="na">payload</span><span class="p">:</span> <span class="nx">data</span> <span class="p">}))</span> <span class="p">.</span><span class="k">catch</span><span class="p">((</span><span class="nx">error</span><span class="p">)</span> <span class="o">=></span> <span class="nf">dispatch</span><span class="p">({</span> <span class="na">type</span><span class="p">:</span> <span class="dl">'</span><span class="s1">FETCH_DATA_FAILURE</span><span class="dl">'</span><span class="p">,</span> <span class="na">payload</span><span class="p">:</span> <span class="nx">error</span> <span class="p">}));</span> <span class="p">};</span> <span class="p">};</span> </code></pre> </li> </ul> <ol> <li> <p><strong>CSS Modules Example:</strong></p> <ul> <li>Styles in <code>styles.module.css</code>: </li> </ul> <pre class="highlight css"><code> <span class="nc">.button</span> <span class="p">{</span> <span class="nl">background-color</span><span class="p">:</span> <span class="m">#3498db</span><span class="p">;</span> <span class="nl">color</span><span class="p">:</span> <span class="m">#fff</span><span class="p">;</span> <span class="nl">padding</span><span class="p">:</span> <span class="m">10px</span><span class="p">;</span> <span class="nl">border</span><span class="p">:</span> <span class="nb">none</span><span class="p">;</span> <span class="nl">border-radius</span><span class="p">:</span> <span class="m">4px</span><span class="p">;</span> <span class="nl">cursor</span><span class="p">:</span> <span class="nb">pointer</span><span class="p">;</span> <span class="p">}</span> </code></pre> </li> </ol> <ul> <li> <p>React Component using CSS Modules:<br> </p> <pre class="highlight jsx"><code> <span class="k">import</span> <span class="nx">React</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="nx">styles</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">./styles.module.css</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">Button</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">(</span> <span class="p"><</span><span class="nt">button</span> <span class="na">className</span><span class="p">=</span><span class="si">{</span><span class="nx">styles</span><span class="p">.</span><span class="nx">button</span><span class="si">}</span> <span class="na">onClick</span><span class="p">=</span><span class="si">{</span><span class="p">()</span> <span class="o">=></span> <span class="nx">console</span><span class="p">.</span><span class="nf">log</span><span class="p">(</span><span class="dl">'</span><span class="s1">Button clicked</span><span class="dl">'</span><span class="p">)</span><span class="si">}</span><span class="p">></span> Click me <span class="p"></</span><span class="nt">button</span><span class="p">></span> <span class="p">);</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">Button</span><span class="p">;</span> </code></pre> </li> </ul> <ol> <li> <p><strong>Server-Side Rendering (SSR) Example:</strong></p> <ul> <li>Server-side entry point: </li> </ul> <pre class="highlight jsx"><code> <span class="k">import</span> <span class="nx">express</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">express</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="nx">React</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="p">{</span> <span class="nx">renderToString</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react-dom/server</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="nx">App</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">./App</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">server</span> <span class="o">=</span> <span class="nf">express</span><span class="p">();</span> <span class="nx">server</span><span class="p">.</span><span class="nf">get</span><span class="p">(</span><span class="dl">'</span><span class="s1">/</span><span class="dl">'</span><span class="p">,</span> <span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="nx">res</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span> <span class="kd">const</span> <span class="nx">appString</span> <span class="o">=</span> <span class="nf">renderToString</span><span class="p">(<</span><span class="nc">App</span> <span class="p">/>);</span> <span class="nx">res</span><span class="p">.</span><span class="nf">send</span><span class="p">(</span><span class="s2">` <!DOCTYPE html> <html> <head> <title>React SSR Example</title> </head> <body> <div id="app"></span><span class="p">${</span><span class="nx">appString</span><span class="p">}</span><span class="s2"></div> <script src="/bundle.js"></script> </body> </html> `</span><span class="p">);</span> <span class="p">});</span> <span class="nx">server</span><span class="p">.</span><span class="nf">listen</span><span class="p">(</span><span class="mi">3000</span><span class="p">,</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="nx">console</span><span class="p">.</span><span class="nf">log</span><span class="p">(</span><span class="dl">'</span><span class="s1">Server is listening on port 3000</span><span class="dl">'</span><span class="p">);</span> <span class="p">});</span> </code></pre> </li> </ol> <ul> <li> <p>Client-side entry point (bundle.js):<br> </p> <pre class="highlight jsx"><code> <span class="k">import</span> <span class="nx">React</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="nx">ReactDOM</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react-dom</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="nx">App</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">./App</span><span class="dl">'</span><span class="p">;</span> <span class="nx">ReactDOM</span><span class="p">.</span><span class="nf">hydrate</span><span class="p">(<</span><span class="nc">App</span> <span class="p">/>,</span> <span class="nb">document</span><span class="p">.</span><span class="nf">getElementById</span><span class="p">(</span><span class="dl">'</span><span class="s1">app</span><span class="dl">'</span><span class="p">));</span> </code></pre> </li> </ul> <p>These examples cover React Router, Redux Thunk, CSS Modules, and Server-Side Rendering, showcasing various aspects of React and Redux ecosystem.</p> <p>Certainly, let's explore additional topics with corresponding example code:</p> <ol> <li> <p><strong>GraphQL Example with Apollo Client:</strong></p> <ul> <li>Installation: </li> </ul> <pre class="highlight shell"><code> npm <span class="nb">install</span> @apollo/client graphql </code></pre> </li> </ol> <ul> <li> <p>Usage (Fetching data with GraphQL):<br> </p> <pre class="highlight jsx"><code> <span class="k">import</span> <span class="nx">React</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="p">{</span> <span class="nx">useQuery</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">@apollo/client</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="p">{</span> <span class="nx">gql</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">graphql-tag</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">GET_USERS</span> <span class="o">=</span> <span class="nx">gql</span><span class="s2">` query { users { id name } } `</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">UserList</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="kd">const</span> <span class="p">{</span> <span class="nx">loading</span><span class="p">,</span> <span class="nx">error</span><span class="p">,</span> <span class="nx">data</span> <span class="p">}</span> <span class="o">=</span> <span class="nf">useQuery</span><span class="p">(</span><span class="nx">GET_USERS</span><span class="p">);</span> <span class="k">if </span><span class="p">(</span><span class="nx">loading</span><span class="p">)</span> <span class="k">return</span> <span class="p"><</span><span class="nt">p</span><span class="p">></span>Loading...<span class="p"></</span><span class="nt">p</span><span class="p">>;</span> <span class="k">if </span><span class="p">(</span><span class="nx">error</span><span class="p">)</span> <span class="k">return</span> <span class="p"><</span><span class="nt">p</span><span class="p">></span>Error: <span class="si">{</span><span class="nx">error</span><span class="p">.</span><span class="nx">message</span><span class="si">}</span><span class="p"></</span><span class="nt">p</span><span class="p">>;</span> <span class="k">return </span><span class="p">(</span> <span class="p"><</span><span class="nt">ul</span><span class="p">></span> <span class="si">{</span><span class="nx">data</span><span class="p">.</span><span class="nx">users</span><span class="p">.</span><span class="nf">map</span><span class="p">((</span><span class="nx">user</span><span class="p">)</span> <span class="o">=></span> <span class="p">(</span> <span class="p"><</span><span class="nt">li</span> <span class="na">key</span><span class="p">=</span><span class="si">{</span><span class="nx">user</span><span class="p">.</span><span class="nx">id</span><span class="si">}</span><span class="p">></span><span class="si">{</span><span class="nx">user</span><span class="p">.</span><span class="nx">name</span><span class="si">}</span><span class="p"></</span><span class="nt">li</span><span class="p">></span> <span class="p">))</span><span class="si">}</span> <span class="p"></</span><span class="nt">ul</span><span class="p">></span> <span class="p">);</span> <span class="p">};</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">UserList</span><span class="p">;</span> </code></pre> </li> </ul> <ol> <li> <p><strong>WebSocket Example with Socket.io:</strong></p> <ul> <li>Installation: </li> </ul> <pre class="highlight shell"><code> npm <span class="nb">install </span>socket.io-client </code></pre> </li> </ol> <ul> <li> <p>Usage (Connecting to a WebSocket server):<br> </p> <pre class="highlight jsx"><code> <span class="k">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">useEffect</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="nx">io</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">socket.io-client</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">ChatApp</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="nf">useEffect</span><span class="p">(()</span> <span class="o">=></span> <span class="p">{</span> <span class="kd">const</span> <span class="nx">socket</span> <span class="o">=</span> <span class="nf">io</span><span class="p">(</span><span class="dl">'</span><span class="s1">http://localhost:3001</span><span class="dl">'</span><span class="p">);</span> <span class="nx">socket</span><span class="p">.</span><span class="nf">on</span><span class="p">(</span><span class="dl">'</span><span class="s1">message</span><span class="dl">'</span><span class="p">,</span> <span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span> <span class="nx">console</span><span class="p">.</span><span class="nf">log</span><span class="p">(</span><span class="dl">'</span><span class="s1">Received message:</span><span class="dl">'</span><span class="p">,</span> <span class="nx">data</span><span class="p">);</span> <span class="p">});</span> <span class="c1">// Clean up the socket connection on component unmount</span> <span class="k">return </span><span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="nx">socket</span><span class="p">.</span><span class="nf">disconnect</span><span class="p">();</span> <span class="p">};</span> <span class="p">},</span> <span class="p">[]);</span> <span class="k">return</span> <span class="p"><</span><span class="nt">div</span><span class="p">></span>WebSocket Chat App<span class="p"></</span><span class="nt">div</span><span class="p">>;</span> <span class="p">};</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">ChatApp</span><span class="p">;</span> </code></pre> </li> </ul> <ol> <li> <p><strong>Firebase Authentication Example:</strong></p> <ul> <li>Installation: </li> </ul> <pre class="highlight shell"><code> npm <span class="nb">install </span>firebase </code></pre> </li> </ol> <ul> <li> <p>Usage (Firebase authentication in React):<br> </p> <pre class="highlight jsx"><code> <span class="k">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">useState</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="nx">firebase</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">firebase/app</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="dl">'</span><span class="s1">firebase/auth</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">firebaseConfig</span> <span class="o">=</span> <span class="p">{</span> <span class="na">apiKey</span><span class="p">:</span> <span class="dl">'</span><span class="s1">your-api-key</span><span class="dl">'</span><span class="p">,</span> <span class="na">authDomain</span><span class="p">:</span> <span class="dl">'</span><span class="s1">your-auth-domain</span><span class="dl">'</span><span class="p">,</span> <span class="c1">// ... other config options</span> <span class="p">};</span> <span class="nx">firebase</span><span class="p">.</span><span class="nf">initializeApp</span><span class="p">(</span><span class="nx">firebaseConfig</span><span class="p">);</span> <span class="kd">const</span> <span class="nx">AuthExample</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="kd">const</span> <span class="p">[</span><span class="nx">user</span><span class="p">,</span> <span class="nx">setUser</span><span class="p">]</span> <span class="o">=</span> <span class="nf">useState</span><span class="p">(</span><span class="kc">null</span><span class="p">);</span> <span class="kd">const</span> <span class="nx">handleSignIn</span> <span class="o">=</span> <span class="k">async </span><span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="kd">const</span> <span class="nx">provider</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">firebase</span><span class="p">.</span><span class="nx">auth</span><span class="p">.</span><span class="nc">GoogleAuthProvider</span><span class="p">();</span> <span class="k">try</span> <span class="p">{</span> <span class="kd">const</span> <span class="nx">result</span> <span class="o">=</span> <span class="k">await</span> <span class="nx">firebase</span><span class="p">.</span><span class="nf">auth</span><span class="p">().</span><span class="nf">signInWithPopup</span><span class="p">(</span><span class="nx">provider</span><span class="p">);</span> <span class="nf">setUser</span><span class="p">(</span><span class="nx">result</span><span class="p">.</span><span class="nx">user</span><span class="p">);</span> <span class="p">}</span> <span class="k">catch </span><span class="p">(</span><span class="nx">error</span><span class="p">)</span> <span class="p">{</span> <span class="nx">console</span><span class="p">.</span><span class="nf">error</span><span class="p">(</span><span class="dl">'</span><span class="s1">Authentication error:</span><span class="dl">'</span><span class="p">,</span> <span class="nx">error</span><span class="p">);</span> <span class="p">}</span> <span class="p">};</span> <span class="k">return </span><span class="p">(</span> <span class="p"><</span><span class="nt">div</span><span class="p">></span> <span class="si">{</span><span class="nx">user</span> <span class="p">?</span> <span class="p">(</span> <span class="p"><</span><span class="nt">p</span><span class="p">></span>Welcome, <span class="si">{</span><span class="nx">user</span><span class="p">.</span><span class="nx">displayName</span><span class="si">}</span>!<span class="p"></</span><span class="nt">p</span><span class="p">></span> <span class="p">)</span> <span class="p">:</span> <span class="p">(</span> <span class="p"><</span><span class="nt">button</span> <span class="na">onClick</span><span class="p">=</span><span class="si">{</span><span class="nx">handleSignIn</span><span class="si">}</span><span class="p">></span>Sign in with Google<span class="p"></</span><span class="nt">button</span><span class="p">></span> <span class="p">)</span><span class="si">}</span> <span class="p"></</span><span class="nt">div</span><span class="p">></span> <span class="p">);</span> <span class="p">};</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">AuthExample</span><span class="p">;</span> </code></pre> </li> </ul> <p>These examples cover GraphQL integration with Apollo Client, WebSocket communication with Socket.io, and Firebase authentication in React. Feel free to explore and adapt these examples to your specific use cases.</p> <p>Certainly, let's continue exploring more topics with corresponding example code:</p> <ol> <li> <p><strong>Testing with Jest and Enzyme:</strong></p> <ul> <li>Installation: </li> </ul> <pre class="highlight shell"><code> npm <span class="nb">install </span>jest enzyme enzyme-to-json enzyme-adapter-react-16 <span class="nt">--save-dev</span> </code></pre> </li> </ol> <div class="highlight js-code-highlight"> <pre class="highlight plaintext"><code>- Usage (Sample test using Jest and Enzyme): </code></pre> </div> <div class="highlight js-code-highlight"> <pre class="highlight plaintext"><code> ```jsx import React from 'react'; import { shallow } from 'enzyme'; import toJson from 'enzyme-to-json'; import MyComponent from './MyComponent'; describe('MyComponent', () => { it('renders correctly', () => { const wrapper = shallow(<MyComponent />); expect(toJson(wrapper)).toMatchSnapshot(); }); it('handles click event', () => { const mockOnClick = jest.fn(); const wrapper = shallow(<MyComponent onClick={mockOnClick} />); wrapper.find('button').simulate('click'); expect(mockOnClick).toHaveBeenCalledTimes(1); }); }); ``` </code></pre> </div> <ol> <li> <p><strong>Storybook Example:</strong></p> <ul> <li>Installation: </li> </ul> <pre class="highlight shell"><code> npx sb init </code></pre> </li> </ol> <div class="highlight js-code-highlight"> <pre class="highlight plaintext"><code>- Usage (Creating a simple story in Storybook): - Create a `Button.js` component: </code></pre> </div> <div class="highlight js-code-highlight"> <pre class="highlight plaintext"><code> ```jsx import React from 'react'; const Button = ({ label, onClick }) => ( <button onClick={onClick}>{label}</button> ); export default Button; ``` </code></pre> </div> <div class="highlight js-code-highlight"> <pre class="highlight plaintext"><code> - Create a `Button.stories.js` file: </code></pre> </div> <div class="highlight js-code-highlight"> <pre class="highlight plaintext"><code> ```jsx import React from 'react'; import Button from './Button'; export default { title: 'Button', component: Button, }; const Template = (args) => <Button {...args} />; export const Primary = Template.bind({}); Primary.args = { label: 'Primary Button', onClick: () => console.log('Button clicked'), }; ``` </code></pre> </div> <div class="highlight js-code-highlight"> <pre class="highlight plaintext"><code> - Run Storybook: </code></pre> </div> <div class="highlight js-code-highlight"> <pre class="highlight plaintext"><code> ```bash npx sb ``` </code></pre> </div> <ol> <li> <p><strong>Code Splitting with React Lazy and Suspense:</strong></p> <ul> <li>Usage (Lazy loading a component with React Lazy and Suspense): </li> </ul> <pre class="highlight jsx"><code> <span class="k">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">lazy</span><span class="p">,</span> <span class="nx">Suspense</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">LazyComponent</span> <span class="o">=</span> <span class="nf">lazy</span><span class="p">(()</span> <span class="o">=></span> <span class="k">import</span><span class="p">(</span><span class="dl">'</span><span class="s1">./LazyComponent</span><span class="dl">'</span><span class="p">));</span> <span class="kd">const</span> <span class="nx">App</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">(</span> <span class="p"><</span><span class="nt">div</span><span class="p">></span> <span class="p"><</span><span class="nc">Suspense</span> <span class="na">fallback</span><span class="p">=</span><span class="si">{</span><span class="p"><</span><span class="nt">div</span><span class="p">></span>Loading...<span class="p"></</span><span class="nt">div</span><span class="p">></span><span class="si">}</span><span class="p">></span> <span class="p"><</span><span class="nc">LazyComponent</span> <span class="p">/></span> <span class="p"></</span><span class="nc">Suspense</span><span class="p">></span> <span class="p"></</span><span class="nt">div</span><span class="p">></span> <span class="p">);</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">App</span><span class="p">;</span> </code></pre> </li> <li> <p><strong>CSS-in-JS with styled-components:</strong></p> <ul> <li>Installation: </li> </ul> <pre class="highlight shell"><code> npm <span class="nb">install </span>styled-components </code></pre> </li> </ol> <div class="highlight js-code-highlight"> <pre class="highlight plaintext"><code>- Usage (Styling a component with styled-components): </code></pre> </div> <div class="highlight js-code-highlight"> <pre class="highlight plaintext"><code> ```jsx import React from 'react'; import styled from 'styled-components'; const StyledButton = styled.button` background-color: #3498db; color: #fff; padding: 10px; border: none; border-radius: 4px; cursor: pointer; `; const StyledComponent = () => ( <StyledButton onClick={() => console.log('Styled button clicked')}> Click me </StyledButton> ); export default StyledComponent; ``` </code></pre> </div> <p>Feel free to explore these examples to gain insights into testing with Jest and Enzyme, using Storybook for component development, implementing code splitting with React Lazy and Suspense, and styling components with CSS-in-JS using styled-components.</p> <p>Certainly, let's explore additional topics with corresponding example code:</p> <ol> <li> <p><strong>Responsive Design with CSS Media Queries:</strong></p> <ul> <li>Usage (Creating a responsive layout using CSS media queries): </li> </ul> <pre class="highlight css"><code> <span class="c">/* Regular styles */</span> <span class="nc">.container</span> <span class="p">{</span> <span class="nl">width</span><span class="p">:</span> <span class="m">100%</span><span class="p">;</span> <span class="nl">margin</span><span class="p">:</span> <span class="m">0</span> <span class="nb">auto</span><span class="p">;</span> <span class="p">}</span> <span class="c">/* Media query for screens smaller than 768px */</span> <span class="k">@media</span> <span class="n">screen</span> <span class="n">and</span> <span class="p">(</span><span class="n">max-width</span><span class="p">:</span> <span class="m">768px</span><span class="p">)</span> <span class="p">{</span> <span class="nc">.container</span> <span class="p">{</span> <span class="nl">width</span><span class="p">:</span> <span class="m">90%</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> </code></pre> </li> <li> <p><strong>Error Boundary Example:</strong></p> <ul> <li>Usage (Implementing an error boundary in React): </li> </ul> <pre class="highlight jsx"><code> <span class="k">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">Component</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="kd">class</span> <span class="nc">ErrorBoundary</span> <span class="kd">extends</span> <span class="nc">Component</span> <span class="p">{</span> <span class="nf">constructor</span><span class="p">(</span><span class="nx">props</span><span class="p">)</span> <span class="p">{</span> <span class="k">super</span><span class="p">(</span><span class="nx">props</span><span class="p">);</span> <span class="k">this</span><span class="p">.</span><span class="nx">state</span> <span class="o">=</span> <span class="p">{</span> <span class="na">hasError</span><span class="p">:</span> <span class="kc">false</span> <span class="p">};</span> <span class="p">}</span> <span class="kd">static</span> <span class="nf">getDerivedStateFromError</span><span class="p">(</span><span class="nx">error</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="p">{</span> <span class="na">hasError</span><span class="p">:</span> <span class="kc">true</span> <span class="p">};</span> <span class="p">}</span> <span class="nf">componentDidCatch</span><span class="p">(</span><span class="nx">error</span><span class="p">,</span> <span class="nx">errorInfo</span><span class="p">)</span> <span class="p">{</span> <span class="nf">logErrorToService</span><span class="p">(</span><span class="nx">error</span><span class="p">,</span> <span class="nx">errorInfo</span><span class="p">);</span> <span class="p">}</span> <span class="nf">render</span><span class="p">()</span> <span class="p">{</span> <span class="k">if </span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">state</span><span class="p">.</span><span class="nx">hasError</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="p"><</span><span class="nt">h1</span><span class="p">></span>Something went wrong.<span class="p"></</span><span class="nt">h1</span><span class="p">>;</span> <span class="p">}</span> <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">props</span><span class="p">.</span><span class="nx">children</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">ErrorBoundary</span><span class="p">;</span> </code></pre> </li> <li> <p><strong>JWT Authentication in React:</strong></p> <ul> <li>Usage (Handling JWT authentication in a React app): </li> </ul> <pre class="highlight jsx"><code> <span class="k">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">useState</span><span class="p">,</span> <span class="nx">useEffect</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">App</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="kd">const</span> <span class="p">[</span><span class="nx">user</span><span class="p">,</span> <span class="nx">setUser</span><span class="p">]</span> <span class="o">=</span> <span class="nf">useState</span><span class="p">(</span><span class="kc">null</span><span class="p">);</span> <span class="nf">useEffect</span><span class="p">(()</span> <span class="o">=></span> <span class="p">{</span> <span class="kd">const</span> <span class="nx">token</span> <span class="o">=</span> <span class="nx">localStorage</span><span class="p">.</span><span class="nf">getItem</span><span class="p">(</span><span class="dl">'</span><span class="s1">jwtToken</span><span class="dl">'</span><span class="p">);</span> <span class="k">if </span><span class="p">(</span><span class="nx">token</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// Verify the token on the server and set the user accordingly</span> <span class="nf">verifyToken</span><span class="p">(</span><span class="nx">token</span><span class="p">);</span> <span class="p">}</span> <span class="p">},</span> <span class="p">[]);</span> <span class="kd">const</span> <span class="nx">handleLogin</span> <span class="o">=</span> <span class="p">(</span><span class="nx">token</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span> <span class="c1">// Save token to localStorage and set the user</span> <span class="nx">localStorage</span><span class="p">.</span><span class="nf">setItem</span><span class="p">(</span><span class="dl">'</span><span class="s1">jwtToken</span><span class="dl">'</span><span class="p">,</span> <span class="nx">token</span><span class="p">);</span> <span class="nf">verifyToken</span><span class="p">(</span><span class="nx">token</span><span class="p">);</span> <span class="p">};</span> <span class="kd">const</span> <span class="nx">handleLogout</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="c1">// Remove token from localStorage and reset the user</span> <span class="nx">localStorage</span><span class="p">.</span><span class="nf">removeItem</span><span class="p">(</span><span class="dl">'</span><span class="s1">jwtToken</span><span class="dl">'</span><span class="p">);</span> <span class="nf">setUser</span><span class="p">(</span><span class="kc">null</span><span class="p">);</span> <span class="p">};</span> <span class="kd">const</span> <span class="nx">verifyToken</span> <span class="o">=</span> <span class="p">(</span><span class="nx">token</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span> <span class="c1">// Send token to server for verification</span> <span class="c1">// Set the user if verification is successful</span> <span class="p">};</span> <span class="k">return </span><span class="p">(</span> <span class="p"><</span><span class="nt">div</span><span class="p">></span> <span class="si">{</span><span class="nx">user</span> <span class="p">?</span> <span class="p">(</span> <span class="p"><</span><span class="nt">button</span> <span class="na">onClick</span><span class="p">=</span><span class="si">{</span><span class="nx">handleLogout</span><span class="si">}</span><span class="p">></span>Logout<span class="p"></</span><span class="nt">button</span><span class="p">></span> <span class="p">)</span> <span class="p">:</span> <span class="p">(</span> <span class="p"><</span><span class="nt">button</span> <span class="na">onClick</span><span class="p">=</span><span class="si">{</span><span class="p">()</span> <span class="o">=></span> <span class="nf">handleLogin</span><span class="p">(</span><span class="dl">'</span><span class="s1">exampleToken</span><span class="dl">'</span><span class="p">)</span><span class="si">}</span><span class="p">></span>Login<span class="p"></</span><span class="nt">button</span><span class="p">></span> <span class="p">)</span><span class="si">}</span> <span class="p"></</span><span class="nt">div</span><span class="p">></span> <span class="p">);</span> <span class="p">};</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">App</span><span class="p">;</span> </code></pre> </li> <li> <p><strong>Serverless Function Example:</strong></p> <ul> <li> <p>Usage (Creating a serverless function with Netlify Functions):</p> <ul> <li>Create a file named <code>hello.js</code> in the <code>netlify/functions</code> directory: </li> </ul> <pre class="highlight javascript"><code><span class="nx">exports</span><span class="p">.</span><span class="nx">handler</span> <span class="o">=</span> <span class="k">async </span><span class="p">(</span><span class="nx">event</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span> <span class="k">return</span> <span class="p">{</span> <span class="na">statusCode</span><span class="p">:</span> <span class="mi">200</span><span class="p">,</span> <span class="na">body</span><span class="p">:</span> <span class="nx">JSON</span><span class="p">.</span><span class="nf">stringify</span><span class="p">({</span> <span class="na">message</span><span class="p">:</span> <span class="dl">'</span><span class="s1">Hello, World!</span><span class="dl">'</span> <span class="p">}),</span> <span class="p">};</span> <span class="p">};</span> </code></pre> </li> </ul> </li> </ol> <div class="highlight js-code-highlight"> <pre class="highlight plaintext"><code> - Deploy your site to Netlify, and the function becomes available at `/.netlify/functions/hello`. </code></pre> </div> <p>Feel free to explore these examples covering responsive design with CSS media queries, implementing an error boundary in React, handling JWT authentication, and creating a serverless function using Netlify Functions.</p> <p>Certainly, let's delve into additional topics with corresponding example code:</p> <ol> <li> <p><strong>Redux Persist for Persistent State:</strong></p> <ul> <li>Installation: </li> </ul> <pre class="highlight shell"><code> npm <span class="nb">install </span>redux-persist </code></pre> </li> </ol> <ul> <li> <p>Usage (Persisting Redux state to local storage):<br> </p> <pre class="highlight jsx"><code> <span class="k">import</span> <span class="p">{</span> <span class="nx">createStore</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">redux</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="p">{</span> <span class="nx">persistStore</span><span class="p">,</span> <span class="nx">persistReducer</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">redux-persist</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="nx">storage</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">redux-persist/lib/storage</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="nx">rootReducer</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">./reducers</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">persistConfig</span> <span class="o">=</span> <span class="p">{</span> <span class="na">key</span><span class="p">:</span> <span class="dl">'</span><span class="s1">root</span><span class="dl">'</span><span class="p">,</span> <span class="nx">storage</span><span class="p">,</span> <span class="p">};</span> <span class="kd">const</span> <span class="nx">persistedReducer</span> <span class="o">=</span> <span class="nf">persistReducer</span><span class="p">(</span><span class="nx">persistConfig</span><span class="p">,</span> <span class="nx">rootReducer</span><span class="p">);</span> <span class="kd">const</span> <span class="nx">store</span> <span class="o">=</span> <span class="nf">createStore</span><span class="p">(</span><span class="nx">persistedReducer</span><span class="p">);</span> <span class="kd">const</span> <span class="nx">persistor</span> <span class="o">=</span> <span class="nf">persistStore</span><span class="p">(</span><span class="nx">store</span><span class="p">);</span> <span class="k">export</span> <span class="p">{</span> <span class="nx">store</span><span class="p">,</span> <span class="nx">persistor</span> <span class="p">};</span> </code></pre> </li> </ul> <ol> <li> <p><strong>Internationalization (i18n) Example with react-intl:</strong></p> <ul> <li>Installation: </li> </ul> <pre class="highlight shell"><code> npm <span class="nb">install </span>react-intl </code></pre> </li> </ol> <ul> <li> <p>Usage (Implementing internationalization in a React app):<br> </p> <pre class="highlight jsx"><code> <span class="k">import</span> <span class="nx">React</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="p">{</span> <span class="nx">FormattedMessage</span><span class="p">,</span> <span class="nx">IntlProvider</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react-intl</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">messages</span> <span class="o">=</span> <span class="p">{</span> <span class="na">en</span><span class="p">:</span> <span class="p">{</span> <span class="na">greeting</span><span class="p">:</span> <span class="dl">'</span><span class="s1">Hello!</span><span class="dl">'</span><span class="p">,</span> <span class="p">},</span> <span class="na">fr</span><span class="p">:</span> <span class="p">{</span> <span class="na">greeting</span><span class="p">:</span> <span class="dl">'</span><span class="s1">Bonjour!</span><span class="dl">'</span><span class="p">,</span> <span class="p">},</span> <span class="p">};</span> <span class="kd">const</span> <span class="nx">App</span> <span class="o">=</span> <span class="p">({</span> <span class="nx">locale</span> <span class="p">})</span> <span class="o">=></span> <span class="p">(</span> <span class="p"><</span><span class="nc">IntlProvider</span> <span class="na">locale</span><span class="p">=</span><span class="si">{</span><span class="nx">locale</span><span class="si">}</span> <span class="na">messages</span><span class="p">=</span><span class="si">{</span><span class="nx">messages</span><span class="p">[</span><span class="nx">locale</span><span class="p">]</span><span class="si">}</span><span class="p">></span> <span class="p"><</span><span class="nt">div</span><span class="p">></span> <span class="p"><</span><span class="nc">FormattedMessage</span> <span class="na">id</span><span class="p">=</span><span class="s">"greeting"</span> <span class="p">/></span> <span class="p"></</span><span class="nt">div</span><span class="p">></span> <span class="p"></</span><span class="nc">IntlProvider</span><span class="p">></span> <span class="p">);</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">App</span><span class="p">;</span> </code></pre> </li> </ul> <ol> <li> <p><strong>Integration with WebSockets using Socket.io:</strong></p> <ul> <li>Installation: </li> </ul> <pre class="highlight shell"><code> npm <span class="nb">install </span>socket.io-client </code></pre> </li> </ol> <ul> <li> <p>Usage (Connecting to a WebSocket server with Socket.io):<br> </p> <pre class="highlight jsx"><code> <span class="k">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">useEffect</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="nx">io</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">socket.io-client</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">ChatApp</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="nf">useEffect</span><span class="p">(()</span> <span class="o">=></span> <span class="p">{</span> <span class="kd">const</span> <span class="nx">socket</span> <span class="o">=</span> <span class="nf">io</span><span class="p">(</span><span class="dl">'</span><span class="s1">http://localhost:3001</span><span class="dl">'</span><span class="p">);</span> <span class="nx">socket</span><span class="p">.</span><span class="nf">on</span><span class="p">(</span><span class="dl">'</span><span class="s1">message</span><span class="dl">'</span><span class="p">,</span> <span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span> <span class="nx">console</span><span class="p">.</span><span class="nf">log</span><span class="p">(</span><span class="dl">'</span><span class="s1">Received message:</span><span class="dl">'</span><span class="p">,</span> <span class="nx">data</span><span class="p">);</span> <span class="p">});</span> <span class="k">return </span><span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="nx">socket</span><span class="p">.</span><span class="nf">disconnect</span><span class="p">();</span> <span class="p">};</span> <span class="p">},</span> <span class="p">[]);</span> <span class="k">return</span> <span class="p"><</span><span class="nt">div</span><span class="p">></span>WebSocket Chat App<span class="p"></</span><span class="nt">div</span><span class="p">>;</span> <span class="p">};</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">ChatApp</span><span class="p">;</span> </code></pre> </li> </ul> <ol> <li> <p><strong>Using React Portals:</strong></p> <ul> <li>Usage (Creating a portal in React): </li> </ul> <pre class="highlight jsx"><code> <span class="k">import</span> <span class="nx">React</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="nx">ReactDOM</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react-dom</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">PortalComponent</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="kd">const</span> <span class="nx">portalRoot</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nf">getElementById</span><span class="p">(</span><span class="dl">'</span><span class="s1">portal-root</span><span class="dl">'</span><span class="p">);</span> <span class="kd">const</span> <span class="nx">portalContent</span> <span class="o">=</span> <span class="p"><</span><span class="nt">div</span><span class="p">></span>Content rendered in portal<span class="p"></</span><span class="nt">div</span><span class="p">>;</span> <span class="k">return</span> <span class="nx">ReactDOM</span><span class="p">.</span><span class="nf">createPortal</span><span class="p">(</span><span class="nx">portalContent</span><span class="p">,</span> <span class="nx">portalRoot</span><span class="p">);</span> <span class="p">};</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">PortalComponent</span><span class="p">;</span> </code></pre> </li> </ol> <p>Feel free to explore these examples covering Redux Persist for persistent state, internationalization with react-intl, integration with WebSockets using Socket.io, and the use of React Portals. Adapt and experiment with these concepts based on your application's requirements.</p> <p>Certainly, let's continue exploring more topics with corresponding example code:</p> <ol> <li> <p><strong>Web Workers in React:</strong></p> <ul> <li>Usage (Creating and using a Web Worker in a React app): </li> </ul> <pre class="highlight jsx"><code> <span class="c1">// main.js</span> <span class="k">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">useEffect</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="nx">MyWorker</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">./my.worker</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">App</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="nf">useEffect</span><span class="p">(()</span> <span class="o">=></span> <span class="p">{</span> <span class="kd">const</span> <span class="nx">worker</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">MyWorker</span><span class="p">();</span> <span class="nx">worker</span><span class="p">.</span><span class="nf">postMessage</span><span class="p">({</span> <span class="na">message</span><span class="p">:</span> <span class="dl">'</span><span class="s1">Hello from main thread!</span><span class="dl">'</span> <span class="p">});</span> <span class="nx">worker</span><span class="p">.</span><span class="nx">onmessage</span> <span class="o">=</span> <span class="p">(</span><span class="nx">event</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span> <span class="nx">console</span><span class="p">.</span><span class="nf">log</span><span class="p">(</span><span class="dl">'</span><span class="s1">Received message from Web Worker:</span><span class="dl">'</span><span class="p">,</span> <span class="nx">event</span><span class="p">.</span><span class="nx">data</span><span class="p">);</span> <span class="p">};</span> <span class="k">return </span><span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="nx">worker</span><span class="p">.</span><span class="nf">terminate</span><span class="p">();</span> <span class="p">};</span> <span class="p">},</span> <span class="p">[]);</span> <span class="k">return</span> <span class="p"><</span><span class="nt">div</span><span class="p">></span>Web Worker Example<span class="p"></</span><span class="nt">div</span><span class="p">>;</span> <span class="p">};</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">App</span><span class="p">;</span> </code></pre> </li> </ol> <div class="highlight js-code-highlight"> <pre class="highlight plaintext"><code> ```js // my.worker.js self.onmessage = (event) => { console.log('Received message in Web Worker:', event.data); self.postMessage({ message: 'Hello from Web Worker!' }); }; ``` </code></pre> </div> <ol> <li> <p><strong>Form Handling in React:</strong></p> <ul> <li>Usage (Handling form input in a React component): </li> </ul> <pre class="highlight jsx"><code> <span class="k">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">useState</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">FormExample</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="kd">const</span> <span class="p">[</span><span class="nx">formData</span><span class="p">,</span> <span class="nx">setFormData</span><span class="p">]</span> <span class="o">=</span> <span class="nf">useState</span><span class="p">({</span> <span class="na">username</span><span class="p">:</span> <span class="dl">''</span><span class="p">,</span> <span class="na">password</span><span class="p">:</span> <span class="dl">''</span><span class="p">,</span> <span class="p">});</span> <span class="kd">const</span> <span class="nx">handleChange</span> <span class="o">=</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span> <span class="nf">setFormData</span><span class="p">({</span> <span class="p">...</span><span class="nx">formData</span><span class="p">,</span> <span class="p">[</span><span class="nx">e</span><span class="p">.</span><span class="nx">target</span><span class="p">.</span><span class="nx">name</span><span class="p">]:</span> <span class="nx">e</span><span class="p">.</span><span class="nx">target</span><span class="p">.</span><span class="nx">value</span> <span class="p">});</span> <span class="p">};</span> <span class="kd">const</span> <span class="nx">handleSubmit</span> <span class="o">=</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span> <span class="nx">e</span><span class="p">.</span><span class="nf">preventDefault</span><span class="p">();</span> <span class="nx">console</span><span class="p">.</span><span class="nf">log</span><span class="p">(</span><span class="dl">'</span><span class="s1">Form submitted with data:</span><span class="dl">'</span><span class="p">,</span> <span class="nx">formData</span><span class="p">);</span> <span class="p">};</span> <span class="k">return </span><span class="p">(</span> <span class="p"><</span><span class="nt">form</span> <span class="na">onSubmit</span><span class="p">=</span><span class="si">{</span><span class="nx">handleSubmit</span><span class="si">}</span><span class="p">></span> <span class="p"><</span><span class="nt">label</span><span class="p">></span> Username: <span class="p"><</span><span class="nt">input</span> <span class="na">type</span><span class="p">=</span><span class="s">"text"</span> <span class="na">name</span><span class="p">=</span><span class="s">"username"</span> <span class="na">value</span><span class="p">=</span><span class="si">{</span><span class="nx">formData</span><span class="p">.</span><span class="nx">username</span><span class="si">}</span> <span class="na">onChange</span><span class="p">=</span><span class="si">{</span><span class="nx">handleChange</span><span class="si">}</span> <span class="p">/></span> <span class="p"></</span><span class="nt">label</span><span class="p">></span> <span class="p"><</span><span class="nt">br</span> <span class="p">/></span> <span class="p"><</span><span class="nt">label</span><span class="p">></span> Password: <span class="p"><</span><span class="nt">input</span> <span class="na">type</span><span class="p">=</span><span class="s">"password"</span> <span class="na">name</span><span class="p">=</span><span class="s">"password"</span> <span class="na">value</span><span class="p">=</span><span class="si">{</span><span class="nx">formData</span><span class="p">.</span><span class="nx">password</span><span class="si">}</span> <span class="na">onChange</span><span class="p">=</span><span class="si">{</span><span class="nx">handleChange</span><span class="si">}</span> <span class="p">/></span> <span class="p"></</span><span class="nt">label</span><span class="p">></span> <span class="p"><</span><span class="nt">br</span> <span class="p">/></span> <span class="p"><</span><span class="nt">button</span> <span class="na">type</span><span class="p">=</span><span class="s">"submit"</span><span class="p">></span>Submit<span class="p"></</span><span class="nt">button</span><span class="p">></span> <span class="p"></</span><span class="nt">form</span><span class="p">></span> <span class="p">);</span> <span class="p">};</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">FormExample</span><span class="p">;</span> </code></pre> </li> <li> <p><strong>React Helmet for Managing Head in React:</strong></p> <ul> <li>Installation: </li> </ul> <pre class="highlight shell"><code> npm <span class="nb">install </span>react-helmet </code></pre> </li> </ol> <ul> <li> <p>Usage (Setting document head properties in a React component):<br> </p> <pre class="highlight jsx"><code> <span class="k">import</span> <span class="nx">React</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="p">{</span> <span class="nx">Helmet</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react-helmet</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">HeadExample</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">(</span> <span class="p"><</span><span class="nt">div</span><span class="p">></span> <span class="p"><</span><span class="nc">Helmet</span><span class="p">></span> <span class="p"><</span><span class="nt">title</span><span class="p">></span>Page Title<span class="p"></</span><span class="nt">title</span><span class="p">></span> <span class="p"><</span><span class="nt">meta</span> <span class="na">name</span><span class="p">=</span><span class="s">"description"</span> <span class="na">content</span><span class="p">=</span><span class="s">"This is a description."</span> <span class="p">/></span> <span class="p"></</span><span class="nc">Helmet</span><span class="p">></span> <span class="p"><</span><span class="nt">h1</span><span class="p">></span>Page Content<span class="p"></</span><span class="nt">h1</span><span class="p">></span> <span class="p"></</span><span class="nt">div</span><span class="p">></span> <span class="p">);</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">HeadExample</span><span class="p">;</span> </code></pre> </li> </ul> <ol> <li> <p><strong>Using React Context API:</strong></p> <ul> <li>Usage (Creating a simple context in React): </li> </ul> <pre class="highlight jsx"><code> <span class="k">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">createContext</span><span class="p">,</span> <span class="nx">useContext</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">MyContext</span> <span class="o">=</span> <span class="nf">createContext</span><span class="p">();</span> <span class="kd">const</span> <span class="nx">ParentComponent</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">(</span> <span class="p"><</span><span class="nc">MyContext</span><span class="p">.</span><span class="nc">Provider</span> <span class="na">value</span><span class="p">=</span><span class="si">{</span><span class="p">{</span> <span class="na">message</span><span class="p">:</span> <span class="dl">'</span><span class="s1">Hello from Context!</span><span class="dl">'</span> <span class="p">}</span><span class="si">}</span><span class="p">></span> <span class="p"><</span><span class="nc">ChildComponent</span> <span class="p">/></span> <span class="p"></</span><span class="nc">MyContext</span><span class="p">.</span><span class="nc">Provider</span><span class="p">></span> <span class="p">);</span> <span class="kd">const</span> <span class="nx">ChildComponent</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="kd">const</span> <span class="nx">contextValue</span> <span class="o">=</span> <span class="nf">useContext</span><span class="p">(</span><span class="nx">MyContext</span><span class="p">);</span> <span class="k">return</span> <span class="p"><</span><span class="nt">p</span><span class="p">></span><span class="si">{</span><span class="nx">contextValue</span><span class="p">.</span><span class="nx">message</span><span class="si">}</span><span class="p"></</span><span class="nt">p</span><span class="p">>;</span> <span class="p">};</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">ParentComponent</span><span class="p">;</span> </code></pre> </li> </ol> <p>Feel free to explore these examples covering Web Workers in React, form handling, managing document head properties with React Helmet, and using the React Context API. Experiment with these concepts based on your application's needs.</p> <p>Certainly, let's continue exploring more topics with corresponding example code:</p> <ol> <li> <p><strong>Debouncing and Throttling in React:</strong></p> <ul> <li>Usage (Implementing debouncing and throttling in a React component): </li> </ul> <pre class="highlight jsx"><code> <span class="k">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">useState</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="k">import</span> <span class="p">{</span> <span class="nx">debounce</span><span class="p">,</span> <span class="nx">throttle</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">lodash</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">DebounceThrottleExample</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="kd">const</span> <span class="p">[</span><span class="nx">value</span><span class="p">,</span> <span class="nx">setValue</span><span class="p">]</span> <span class="o">=</span> <span class="nf">useState</span><span class="p">(</span><span class="dl">''</span><span class="p">);</span> <span class="kd">const</span> <span class="nx">debouncedHandleChange</span> <span class="o">=</span> <span class="nf">debounce</span><span class="p">((</span><span class="nx">input</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span> <span class="nx">console</span><span class="p">.</span><span class="nf">log</span><span class="p">(</span><span class="dl">'</span><span class="s1">Debounced Input:</span><span class="dl">'</span><span class="p">,</span> <span class="nx">input</span><span class="p">);</span> <span class="p">},</span> <span class="mi">1000</span><span class="p">);</span> <span class="kd">const</span> <span class="nx">throttledHandleChange</span> <span class="o">=</span> <span class="nf">throttle</span><span class="p">((</span><span class="nx">input</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span> <span class="nx">console</span><span class="p">.</span><span class="nf">log</span><span class="p">(</span><span class="dl">'</span><span class="s1">Throttled Input:</span><span class="dl">'</span><span class="p">,</span> <span class="nx">input</span><span class="p">);</span> <span class="p">},</span> <span class="mi">1000</span><span class="p">);</span> <span class="kd">const</span> <span class="nx">handleChange</span> <span class="o">=</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span> <span class="nf">setValue</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">target</span><span class="p">.</span><span class="nx">value</span><span class="p">);</span> <span class="nf">debouncedHandleChange</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">target</span><span class="p">.</span><span class="nx">value</span><span class="p">);</span> <span class="nf">throttledHandleChange</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">target</span><span class="p">.</span><span class="nx">value</span><span class="p">);</span> <span class="p">};</span> <span class="k">return </span><span class="p">(</span> <span class="p"><</span><span class="nt">div</span><span class="p">></span> <span class="p"><</span><span class="nt">input</span> <span class="na">type</span><span class="p">=</span><span class="s">"text"</span> <span class="na">value</span><span class="p">=</span><span class="si">{</span><span class="nx">value</span><span class="si">}</span> <span class="na">onChange</span><span class="p">=</span><span class="si">{</span><span class="nx">handleChange</span><span class="si">}</span> <span class="p">/></span> <span class="p"></</span><span class="nt">div</span><span class="p">></span> <span class="p">);</span> <span class="p">};</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">DebounceThrottleExample</span><span class="p">;</span> </code></pre> </li> <li> <p><strong>Intersection Observer in React:</strong></p> <ul> <li>Usage (Using Intersection Observer in a React component): </li> </ul> <pre class="highlight jsx"><code> <span class="k">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">useEffect</span><span class="p">,</span> <span class="nx">useRef</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">IntersectionObserverExample</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="kd">const</span> <span class="nx">targetRef</span> <span class="o">=</span> <span class="nf">useRef</span><span class="p">(</span><span class="kc">null</span><span class="p">);</span> <span class="kd">const</span> <span class="nx">handleIntersection</span> <span class="o">=</span> <span class="p">(</span><span class="nx">entries</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span> <span class="nx">entries</span><span class="p">.</span><span class="nf">forEach</span><span class="p">((</span><span class="nx">entry</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span> <span class="k">if </span><span class="p">(</span><span class="nx">entry</span><span class="p">.</span><span class="nx">isIntersecting</span><span class="p">)</span> <span class="p">{</span> <span class="nx">console</span><span class="p">.</span><span class="nf">log</span><span class="p">(</span><span class="dl">'</span><span class="s1">Target is in view!</span><span class="dl">'</span><span class="p">);</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="nx">console</span><span class="p">.</span><span class="nf">log</span><span class="p">(</span><span class="dl">'</span><span class="s1">Target is out of view!</span><span class="dl">'</span><span class="p">);</span> <span class="p">}</span> <span class="p">});</span> <span class="p">};</span> <span class="nf">useEffect</span><span class="p">(()</span> <span class="o">=></span> <span class="p">{</span> <span class="kd">const</span> <span class="nx">observer</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">IntersectionObserver</span><span class="p">(</span><span class="nx">handleIntersection</span><span class="p">);</span> <span class="nx">observer</span><span class="p">.</span><span class="nf">observe</span><span class="p">(</span><span class="nx">targetRef</span><span class="p">.</span><span class="nx">current</span><span class="p">);</span> <span class="k">return </span><span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="nx">observer</span><span class="p">.</span><span class="nf">disconnect</span><span class="p">();</span> <span class="p">};</span> <span class="p">},</span> <span class="p">[]);</span> <span class="k">return</span> <span class="p"><</span><span class="nt">div</span> <span class="na">ref</span><span class="p">=</span><span class="si">{</span><span class="nx">targetRef</span><span class="si">}</span><span class="p">></span>Intersection Observer Target<span class="p"></</span><span class="nt">div</span><span class="p">>;</span> <span class="p">};</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">IntersectionObserverExample</span><span class="p">;</span> </code></pre> </li> <li> <p><strong>Lazy Loading Images in React:</strong></p> <ul> <li>Usage (Implementing lazy loading of images in a React component): </li> </ul> <pre class="highlight jsx"><code> <span class="k">import</span> <span class="nx">React</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">LazyLoadingImage</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">(</span> <span class="p"><</span><span class="nt">div</span> <span class="na">style</span><span class="p">=</span><span class="si">{</span><span class="p">{</span> <span class="na">height</span><span class="p">:</span> <span class="dl">'</span><span class="s1">500px</span><span class="dl">'</span><span class="p">,</span> <span class="na">overflowY</span><span class="p">:</span> <span class="dl">'</span><span class="s1">scroll</span><span class="dl">'</span> <span class="p">}</span><span class="si">}</span><span class="p">></span> <span class="p"><</span><span class="nt">p</span><span class="p">></span>Scroll down to load the image<span class="p"></</span><span class="nt">p</span><span class="p">></span> <span class="p"><</span><span class="nt">img</span> <span class="na">src</span><span class="p">=</span><span class="s">"https://example.com/large-image.jpg"</span> <span class="na">alt</span><span class="p">=</span><span class="s">"Lazy Loaded Image"</span> <span class="na">loading</span><span class="p">=</span><span class="s">"lazy"</span> <span class="p">/></span> <span class="p"></</span><span class="nt">div</span><span class="p">></span> <span class="p">);</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">LazyLoadingImage</span><span class="p">;</span> </code></pre> </li> <li> <p><strong>Memoization in React with useMemo:</strong></p> <ul> <li>Usage (Using <code>useMemo</code> for memoization in a React component): </li> </ul> <pre class="highlight jsx"><code> <span class="k">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">useState</span><span class="p">,</span> <span class="nx">useMemo</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span> <span class="kd">const</span> <span class="nx">MemoizationExample</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span> <span class="kd">const</span> <span class="p">[</span><span class="nx">count</span><span class="p">,</span> <span class="nx">setCount</span><span class="p">]</span> <span class="o">=</span> <span class="nf">useState</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span> <span class="kd">const</span> <span class="nx">squaredValue</span> <span class="o">=</span> <span class="nf">useMemo</span><span class="p">(()</span> <span class="o">=></span> <span class="p">{</span> <span class="nx">console</span><span class="p">.</span><span class="nf">log</span><span class="p">(</span><span class="dl">'</span><span class="s1">Computing squared value...</span><span class="dl">'</span><span class="p">);</span> <span class="k">return</span> <span class="nx">count</span> <span class="o">*</span> <span class="nx">count</span><span class="p">;</span> <span class="p">},</span> <span class="p">[</span><span class="nx">count</span><span class="p">]);</span> <span class="k">return </span><span class="p">(</span> <span class="p"><</span><span class="nt">div</span><span class="p">></span> <span class="p"><</span><span class="nt">p</span><span class="p">></span>Count: <span class="si">{</span><span class="nx">count</span><span class="si">}</span><span class="p"></</span><span class="nt">p</span><span class="p">></span> <span class="p"><</span><span class="nt">p</span><span class="p">></span>Squared Value: <span class="si">{</span><span class="nx">squaredValue</span><span class="si">}</span><span class="p"></</span><span class="nt">p</span><span class="p">></span> <span class="p"><</span><span class="nt">button</span> <span class="na">onClick</span><span class="p">=</span><span class="si">{</span><span class="p">()</span> <span class="o">=></span> <span class="nf">setCount</span><span class="p">(</span><span class="nx">count</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="si">}</span><span class="p">></span>Increment<span class="p"></</span><span class="nt">button</span><span class="p">></span> <span class="p"></</span><span class="nt">div</span><span class="p">></span> <span class="p">);</span> <span class="p">};</span> <span class="k">export</span> <span class="k">default</span> <span class="nx">MemoizationExample</span><span class="p">;</span> </code></pre> </li> </ol> <p>Feel free to explore these examples covering debouncing and throttling, using Intersection Observer, lazy loading images, and memoization with <code>useMemo</code> in React. Adapt and experiment with these concepts based on your application's requirements.</p></div>