<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Cath Leyson</title>
    <description>The latest articles on DEV Community by Cath Leyson (@cathleys).</description>
    <link>https://dev.to/cathleys</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1090876%2F6bc4143e-e1b4-4676-8dcf-2aaa0041a720.jpeg</url>
      <title>DEV Community: Cath Leyson</title>
      <link>https://dev.to/cathleys</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cathleys"/>
    <language>en</language>
    <item>
      <title>How to Create a Loading Spinner in React with styled-components</title>
      <dc:creator>Cath Leyson</dc:creator>
      <pubDate>Sat, 03 Jun 2023 16:00:00 +0000</pubDate>
      <link>https://dev.to/cathleys/how-to-create-a-loading-spinner-in-react-with-styled-components-4p2g</link>
      <guid>https://dev.to/cathleys/how-to-create-a-loading-spinner-in-react-with-styled-components-4p2g</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In modern web applications, it's common to have asynchronous operations that require some time to complete. &lt;/p&gt;

&lt;p&gt;During these operations, it's a good practice to provide visual feedback to the user, indicating that the application is still working. &lt;/p&gt;

&lt;p&gt;One way to achieve this is by using a loading spinner, which is a visual element that rotates to indicate that the application is processing data or performing an action. &lt;/p&gt;

&lt;p&gt;In this tutorial, we'll explore how to create a loading spinner in React using the styled-components library.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Setting up the Project&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before we start implementing the loading spinner, make sure you have a React project set up with styled-components installed. If you haven't already, create a new React project using your preferred method (e.g., Create React App) and install styled-components by running the following command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install styled-components&lt;br&gt;
&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Creating the LoadingSpinner Component&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this step, we'll create a functional component for the loading spinner. Open a new file called like loading-spinner.js and import React and styled-components. &lt;/p&gt;

&lt;p&gt;We'll define two styled components: &lt;code&gt;Container and Loader&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The Container component will center the spinner on the screen, and the Loader component will represent the spinning animation itself.&lt;/p&gt;

&lt;p&gt;Here's the styling for the LoadingSpinner component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import styled from "styled-components";

const Container = styled.div`
  display: flex;
  justify-content: center;
  align-items: center;
`;

 const Loader = styled.div`
  border-width: 0.5rem;
  border-style: solid;
  border-color: purple purple purple purple;
  width: 3.625rem;
  height: 3.625rem;
  border-radius: 50%;
  position: relative;
  -webkit-animation: spin 2s infinite;
  animation: spin 2s infinite;

  &amp;amp;:before,
  &amp;amp;:after {
    content: "";
    width: 0.5rem;
    height: 0.5rem;
    border-radius: 50%;
    background: purple;
    position: absolute;
    left: 0.125rem;
  }

  &amp;amp;:before {
    top: 0.063rem;
  }

  &amp;amp;:after {
    bottom: 0.063rem;
  }

  @keyframes spin {
    100% {
      transform: rotate(360deg);
    }
  }
`;

//Create functional component
export function LoadingSpinner() {

  return (
    &amp;lt;Container&amp;gt;
         &amp;lt;Loader /&amp;gt;
    &amp;lt;/Container&amp;gt;
  );

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3: Using the LoadingSpinner Component&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now that we have our component, we can use it wherever we need to display a loading state in our application. Making this reusable.&lt;/p&gt;

&lt;p&gt;Import the LoadingSpinner component into the file where you want to use it and render it accordingly. &lt;/p&gt;

&lt;p&gt;For example, if you have a component called MyComponent and you want to display the loading spinner while fetching data from an API, you can render it conditionally based on the loading state.&lt;/p&gt;

&lt;p&gt;Here's an example of how I use it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { LoadingSpinner } from "./loading-spinner"

export function MyComponent() {

//I'm using react-query
  const { data, isLoading, isError, error } = useProjects();

  if (isLoading) {
    return &amp;lt;LoadingSpinner /&amp;gt;;
  }

  if (isError)//...


  return (

       //render the array results 

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

&lt;/div&gt;



&lt;p&gt;Hope this helps!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>react</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How Would I Explain React to a kid</title>
      <dc:creator>Cath Leyson</dc:creator>
      <pubDate>Fri, 02 Jun 2023 16:00:00 +0000</pubDate>
      <link>https://dev.to/cathleys/how-would-i-explain-react-to-a-kid-372a</link>
      <guid>https://dev.to/cathleys/how-would-i-explain-react-to-a-kid-372a</guid>
      <description>&lt;p&gt;Like everything else, You should invest time, discipline and motivation learning and understanding how React works. &lt;/p&gt;

&lt;p&gt;But what if a kid suddenly asked you what it is, Of course, you cannot simply throw a college book definition, and let him digest it himself.&lt;/p&gt;

&lt;p&gt;For me, in order to understand something, I need to simplify it like I was five years old. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How React Works&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;React used to build interactive components. It adheres to component-based approach, which allows you to make reusable User Interface elements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Think of it like a lego block&lt;/strong&gt;. You can build any figure of any kind by stacking up lego pieces.&lt;/p&gt;

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

&lt;p&gt;That's the mindset behind React's approach. &lt;/p&gt;

&lt;p&gt;Lego blocks are just these UI components, when put together, you can build web apps made up of components.&lt;/p&gt;

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

&lt;p&gt;I just want to keep this short and simple. That's it.&lt;/p&gt;

&lt;p&gt;Hope this makes sense!&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to Implement Dark Mode in React</title>
      <dc:creator>Cath Leyson</dc:creator>
      <pubDate>Thu, 01 Jun 2023 16:00:00 +0000</pubDate>
      <link>https://dev.to/cathleys/how-to-implement-dark-mode-in-react-256</link>
      <guid>https://dev.to/cathleys/how-to-implement-dark-mode-in-react-256</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Dark mode has become a popular feature in modern web applications, providing users with an alternative color scheme that is easier on the eyes and offers a unique visual experience. &lt;/p&gt;

&lt;p&gt;In this article, we will explore how to implement a dark mode feature in a React application. &lt;/p&gt;

&lt;p&gt;We'll create a theme provider using useContext hook and demonstrate how to toggle between light and dark modes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before proceeding with this tutorial, make sure you have the following installed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;React (version X.X.X or later)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Creating the Theme Provider&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To begin, let's create a file called "theme-provider.tsx" and include the following code:&lt;br&gt;
&lt;/p&gt;

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

type ThemeContextProps = {
  children: React.ReactNode;
};

const defaultContext = {
  isDarkMode: false,
  toggleColorMode: () =&amp;gt; {},
};

export const ThemeContext = createContext(defaultContext);

export function ThemeProvider({ children }: ThemeContextProps) {
  const [isDarkMode, setDarkMode] = useState(defaultContext.isDarkMode);

  return (
    &amp;lt;ThemeContext.Provider
      value={{
        isDarkMode,
        toggleColorMode: () =&amp;gt; setDarkMode((isDark) =&amp;gt; !isDark),
      }}
    &amp;gt;
      {children}
    &amp;lt;/ThemeContext.Provider&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, we create a context using the &lt;code&gt;createContext&lt;/code&gt; function from React. &lt;/p&gt;

&lt;p&gt;We define a default context that includes the initial state of &lt;code&gt;isDarkMode&lt;/code&gt; set to false and a &lt;code&gt;toggleColorMode&lt;/code&gt; function. &lt;/p&gt;

&lt;p&gt;We create a &lt;code&gt;ThemeProvider&lt;/code&gt; component that wraps its children with the context provider. The provider exposes the &lt;code&gt;isDarkMode&lt;/code&gt; state and the &lt;code&gt;toggleColorMode&lt;/code&gt; function to its children.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Integrating the Theme Provider&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Next, let's integrate the theme provider into our application. Assuming you have an &lt;code&gt;my-app.tsx&lt;/code&gt; file and import the ThemeProvider component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { ThemeProvider } from "./theme-provider";

export function MyApp({ Component, pageProps }: AppProps) {
  return (
    &amp;lt;ThemeProvider&amp;gt;
      &amp;lt;Component {...pageProps} /&amp;gt;
    &amp;lt;/ThemeProvider&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, we import the ThemeProvider component and wrap it around the &lt;code&gt;Component&lt;/code&gt; and &lt;code&gt;pageProps&lt;/code&gt; components. This ensures that the theme provider is available throughout your application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using the Theme Context&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, let's consume the theme context in one of your components. Assuming you have a &lt;code&gt;PageContainer&lt;/code&gt; component, import the necessary dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useContext } from "react";
import { ThemeContext } from "./theme-provider";
import { Container, Main } from "./your-styled-components";

type PageProps = {
  children: React.ReactNode;
};

export function PageContainer({ children }: PageProps) {
  const { isDarkMode, toggleColorMode } = useContext(ThemeContext);

  return (
    &amp;lt;&amp;gt;
      {/* Head element and other code */}
      &amp;lt;button onClick={()=&amp;gt; toggleColorMode}&amp;gt;Toggle&amp;lt;/button&amp;gt;
       &amp;lt;Container isDark={isDarkMode}&amp;gt;
        &amp;lt;Main&amp;gt;{children}&amp;lt;/P.Main&amp;gt;
       &amp;lt;/Container&amp;gt;

      {/* Footer component */}
    &amp;lt;/&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, we import the necessary dependencies, including the &lt;code&gt;ThemeContext&lt;/code&gt; and the styled-components (Container and Main).&lt;/p&gt;

&lt;p&gt;Inside the &lt;code&gt;PageContainer&lt;/code&gt; component, we consume the &lt;code&gt;isDarkMode&lt;/code&gt; state from the ThemeContext. &lt;/p&gt;

&lt;p&gt;Based on the value of &lt;code&gt;isDarkMode&lt;/code&gt;, we pass the &lt;code&gt;isDark&lt;/code&gt; prop to the Container component to apply appropriate styling for light or dark mode. &lt;/p&gt;

&lt;p&gt;The children are rendered within the Main component.&lt;/p&gt;

&lt;p&gt;Please replace "your-styled-components" with the actual names of your styled components or use your preferred styling solution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Styling the Dark Mode&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Lastly, to make the dark mode feature work, We need to do the styling. I am using styled-components:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import styled from "styled-component";

export const Container = styled.div&amp;lt;{ isDark: boolean }&amp;gt;`
  background-color: ${({ isDark }) =&amp;gt; (isDark ? "#222" : "#fff")};
`;

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

&lt;/div&gt;



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

&lt;p&gt;Adding a dark mode feature to your React application enhances user experience and provides a more personalized interface. &lt;/p&gt;

&lt;p&gt;By leveraging React's useContext hook, we can easily manage the state of the dark mode throughout the application.&lt;/p&gt;

&lt;p&gt;Hope this helps!&lt;/p&gt;

</description>
      <category>react</category>
      <category>darkmode</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to Create Select with react-select</title>
      <dc:creator>Cath Leyson</dc:creator>
      <pubDate>Wed, 31 May 2023 16:00:00 +0000</pubDate>
      <link>https://dev.to/cathleys/how-to-create-select-with-react-select-33p4</link>
      <guid>https://dev.to/cathleys/how-to-create-select-with-react-select-33p4</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Select dropdown menus are commonly used in web applications to provide users with a list of selectable options. &lt;/p&gt;

&lt;p&gt;In this article, we will explore how to create a select using the &lt;a href="https://react-select.com/home" rel="noopener noreferrer"&gt;react-select&lt;/a&gt; library.&lt;/p&gt;

&lt;p&gt;By leveraging react-select, we can take advantage of its extensive feature set, and so we don't end up creating an inferior version of this library.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before proceeding with this tutorial, ensure that you have the following dependencies installed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;React &lt;code&gt;npx create-react-app&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;react-select &lt;code&gt;npm i --save react-select&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Styling up the Select&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To begin, let's create a custom styles for our select. Create a file and check out the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const customStyles = {

  option: (defaultStyles: any, state: { isSelected: boolean }) =&amp;gt; ({
    ...defaultStyles,
    color: "#101828",
    cursor: "pointer",
    backgroundColor: state.isSelected ? `#ccc` : "#fff",
  }),

  control: (defaultStyles: any, state: { isFocused: boolean }) =&amp;gt; ({
    ...defaultStyles,
    background: "white",
    borderRadius: "0.5rem",
    borderColor: state.isFocused ? "#D6BBFB" : "#D0D5DD",


    "&amp;amp;:hover": {
      borderColor: null,
    },
  }),

  menu: (defaultStyles: any) =&amp;gt; ({
    ...defaultStyles,
    boxShadow:
      "0px 12px 16px -4px rgba(16, 24, 40, 0.1), 0px 4px 6px -2px rgba(16, 24, 40, 0.05)",
  }),


};

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Defining Select Options&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Next, let's define the options for our select dropdown menu. Create a file name, I just called it select-data.ts and include the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const options = [
  { value: "Sleeping", label: "Sleeping" },
  { value: "Ride a banana boat", label: "Ride a banana boat" },
  { value: "Sky diving", label: "Sky diving" },
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, we export an array of options that will be displayed in the select. Each option consists of a value and a label. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2m94x062fh5yu9set8gi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2m94x062fh5yu9set8gi.png" alt="Image description" width="800" height="600"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Implementing the Select&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, let's integrate the select component into our application. Assuming you have a Parent component, import the necessary files we've created before:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React,{ useState } from "react";
import Select from "react-select";
import { customStyles } from "./select.styles";
import { options } from "./select-data";

export function Parent() {
const [selected, setSelected] = useState(null);

  const handleChange = (selectedItem) =&amp;gt; {
    setSelected(selectedItem);
    console.log(`selected option:`, selectedItem);
  };

  return (
    &amp;lt;Select
      options={options}
      placeholder="select..."
      styles={customStyles}
      onChange={handleChange}
      blurInputOnSelect={true}
    /&amp;gt;
    // Rest of the code...
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, we import the Select component and the array from their respective files.&lt;/p&gt;

&lt;p&gt;We define a function, handleChange, to handle the change event when a select option is selected. &lt;/p&gt;

&lt;p&gt;Customize the function to perform the desired action based on the selected option. Finally, we render the Select component, passing the necessary props such as options, placeholder, and onChange.&lt;/p&gt;

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

&lt;p&gt;By the power of react-select, we can create highly customizable select dropdown menus in our React applications. &lt;/p&gt;

&lt;p&gt;The react-select library provides a rich set of features, allows us to apply custom styles and match our design requirements seamlessly. &lt;/p&gt;

&lt;p&gt;With this knowledge, you can enhance user experience by incorporating intuitive selects into your applications.&lt;/p&gt;

&lt;p&gt;Experiment with different styles and options to create selects that best suit your application's needs. &lt;/p&gt;

&lt;p&gt;Hope this helps!&lt;/p&gt;

</description>
      <category>react</category>
      <category>reactselect</category>
    </item>
    <item>
      <title>How to Test URL param with Spaces</title>
      <dc:creator>Cath Leyson</dc:creator>
      <pubDate>Wed, 31 May 2023 04:28:17 +0000</pubDate>
      <link>https://dev.to/cathleys/how-to-test-url-param-with-spaces-100o</link>
      <guid>https://dev.to/cathleys/how-to-test-url-param-with-spaces-100o</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When testing applications that involve URL parameters, it's important to ensure that the parameters are encoded correctly, especially when they contain spaces or special characters. In this article, we will explore how to test URL parameters with spaces in Cypress using a practical example.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting up the Test&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To begin, create a new Cypress test file called "search.spec.ts" in the "cypress/integration" directory. This file will contain our test case for searching a hero name in the search bar.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;describe("Search results", () =&amp;gt; {
  it("types hero name in search bar", () =&amp;gt; {
    cy.visit("http://localhost:3000/home");

    cy.get('[data-cy="search-bar"]')
      .type("iron man")
      .should(
        "have.value",
        "iron man",
        `/characters?nameStartsWith=${encodeURIComponent("iron man")}`
      );
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this test case, we navigate to the homepage and type "iron man" in the search bar. We then assert that the input value matches "iron man" and that the resulting URL contains the encoded parameter "nameStartsWith=iron%20man" (where "%20" represents the space character).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Testing URL Parameters with Spaces&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To test the URL parameter with spaces, we need to ensure that the parameter is correctly encoded before asserting its presence in the URL.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cy.should(
  "have.value",
  "iron man",
  `/characters?nameStartsWith=${encodeURIComponent("iron man")}`
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent"&gt;encodeURIComponent&lt;/a&gt; function converts special characters, including spaces, to their URL-encoded representations. By passing the "iron man" value through this function, we can safely include it in the URL.&lt;/p&gt;

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

&lt;p&gt;Testing URL parameters with spaces is essential to ensure that your web application handles them correctly. &lt;/p&gt;

&lt;p&gt;By using the encodeURIComponent function and asserting the encoded parameter in the URL, you can confidently test scenarios where URL parameters contain spaces or special characters. &lt;/p&gt;

&lt;p&gt;Hope this helps!&lt;/p&gt;

</description>
      <category>testing</category>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to Make Dynamic Routing with Next.js</title>
      <dc:creator>Cath Leyson</dc:creator>
      <pubDate>Tue, 30 May 2023 08:54:02 +0000</pubDate>
      <link>https://dev.to/cathleys/how-to-make-dynamic-routing-with-nextjs-5d8p</link>
      <guid>https://dev.to/cathleys/how-to-make-dynamic-routing-with-nextjs-5d8p</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Dynamic routing is a powerful feature provided by Next.js that allows us to create flexible and scalable applications. &lt;/p&gt;

&lt;p&gt;In this article, we will explore how to implement dynamic routing using Next.js. &lt;/p&gt;

&lt;p&gt;We'll walk through the process of setting up dynamic routes, fetching data for specific routes, and rendering components dynamically based on route parameters. &lt;/p&gt;

&lt;p&gt;Let's dive in!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Folder Structure&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To begin, let's take a look at our folder structure within the pages directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pages
  └─ character
       ├─ [id].tsx
       └─ index.tsx

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

&lt;/div&gt;



&lt;p&gt;In this structure, we have a character folder that contains two files: [id].tsx and index.tsx. The [id].tsx file will be instrumental in setting up our dynamic routing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting Up Dynamic Routing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Within the character folder, the [id].tsx file is responsible for handling the dynamic routing. Here's an example of its contents:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import type { NextPage } from "next";
import * as A from "@features";

const HeroDetailsPage: NextPage = () =&amp;gt; {
  return (
    &amp;lt;A.PageContainer&amp;gt;
      &amp;lt;A.HeroInfoList /&amp;gt;
    &amp;lt;/A.PageContainer&amp;gt;
  );
};

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

&lt;/div&gt;



&lt;p&gt;In this code snippet, we define the "HeroDetailsPage" page, which serves as the template for rendering the details of a specific hero character. &lt;/p&gt;

&lt;p&gt;Inside the component, we can include other components, such as HeroInfoList, to display the relevant information.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fetching Data for Dynamic Routes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To fetch the data for the dynamic routing, we utilize the getHero function, which makes a GET request to retrieve the specific hero's details. &lt;/p&gt;

&lt;p&gt;Here's an example of the getHero function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import axios from "axios";
import { base_url, key, hash } from "@config/keys";
import { useQuery } from "react-query";

async function getHero(id: number) {
  const { data } = await axios.get(`${base_url}/characters/${id}`),{
//other params...
} 
  return data;
}
export function useHero(id: number) {
  return useQuery(["hero details", id], () =&amp;gt; getHero(id));
}

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

&lt;/div&gt;



&lt;p&gt;In this code snippet, we define the getHero function that takes the "id" parameter and makes a GET request to the Marvel API to fetch the details of the character with the corresponding ID. &lt;/p&gt;

&lt;p&gt;The fetched data is returned.&lt;/p&gt;

&lt;p&gt;We also define a custom hook, useHero, which takes the "id" parameter and utilizes the useQuery hook from React Query to handle the data fetching process.&lt;/p&gt;

&lt;p&gt;It passes the id to the getHero function to fetch the data.&lt;/p&gt;

&lt;p&gt;Then, we can utilize the useRouter hook provided by Next.js. &lt;/p&gt;

&lt;p&gt;Let's take a look at the HeroInfoList component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useRouter } from "next/router";
import { useHero } from "@features/hero/api";
import { HeroInfo } from "./hero-info";
import * as A from "@features";

export function HeroInfoList() {
  const router = useRouter();
  const { id } = router.query;
  const { data, isLoading, isError } = useHero(Number(id));

  // Handle loading and error states

  const { results } = data?.data || {};

  return (
    &amp;lt;&amp;gt;
      {results?.map((hero: any) =&amp;gt; (
        &amp;lt;HeroInfo key={hero.id} {...hero} /&amp;gt;
      ))}
      // Rest of the code...
    &amp;lt;/&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;Within the HeroInfoList component, we import the useHero hook from "@features/hero/api" to fetch data for a specific hero. We retrieve the "id" parameter from the router query using Next.js useRouter and pass it to the useHero hook.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rendering Components Dynamically&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Based on the fetched data, we can render components dynamically. In the HeroInfoList component, we iterate over the results array and render a HeroInfo component for each hero.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating Dynamic Links&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To navigate to the dynamic routes, we can use Next.js' built-in link component. Let's take a look at the CharacterCard component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Link from 'next/link';

const Routes =[
characterDetails: "/character/[id]",

//more paths...
]

type CharProps = {
  id: number;
  name: string;
  thumbnail: {
    path: string;
    extension: string;
  };
};

export function CharacterCard({ id, name, thumbnail }: CharProps) {
  return (
    &amp;lt;Link
      href={{
        pathname: Routes.characterDetails,
        query: { id: id },
      }}
    &amp;gt;
      // Rest of the code...
    &amp;lt;/Link&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;The Routes list contains the route paths for our application. We pass the characterDetails route to the pathname property of the link component, along with the "id" parameter as the query object.&lt;/p&gt;

&lt;p&gt;With this setup, when the user clicks on a character card, they will be directed to the HeroDetailsPage component with the corresponding "id" parameter.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So the expected behavior looks like this...&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A user clicks on the CharacterCard Component&lt;/li&gt;
&lt;li&gt;It navigates through characterDetails path with an ID param in the URL.&lt;/li&gt;
&lt;li&gt;It then displays the details about the specific character/hero.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;Dynamic routing is a powerful feature of Next.js that enables us to create dynamic and interactive web applications. &lt;/p&gt;

&lt;p&gt;In this article, we explored the process of setting up dynamic routes, fetching data for specific routes, and rendering components dynamically based on route parameters. &lt;/p&gt;

&lt;p&gt;By leveraging the capabilities of Next.js, we can create flexible and scalable applications that provide an optimal user experience.&lt;/p&gt;

&lt;p&gt;Hope this helps!&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>routing</category>
      <category>react</category>
      <category>reactjsdevelopment</category>
    </item>
    <item>
      <title>How to Fetch Data in React Query Like A Pro</title>
      <dc:creator>Cath Leyson</dc:creator>
      <pubDate>Tue, 30 May 2023 03:39:14 +0000</pubDate>
      <link>https://dev.to/cathleys/how-to-fetch-data-in-react-query-like-a-pro-4f1d</link>
      <guid>https://dev.to/cathleys/how-to-fetch-data-in-react-query-like-a-pro-4f1d</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;In React apps, fetching data from APIs is a common requirement. &lt;/p&gt;

&lt;p&gt;React Query is a powerful library that provides an elegant and efficient way to handle data fetching, caching, and synchronization in your React components. &lt;/p&gt;

&lt;p&gt;In this article, we'll explore how to use React Query to fetch data from the Marvel API and render it in our React application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before we proceed, make sure you have a basic understanding of React and JavaScript. Familiarity with asynchronous operations and RESTful APIs will also be beneficial. &lt;/p&gt;

&lt;p&gt;Check out the &lt;a href="https://developer.marvel.com/docs"&gt;https://developer.marvel.com/docs&lt;/a&gt; first to understand how to make Get requests from Marvel API docs on Swagger.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fetching Data with React Query&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To fetch data from Marvel API, we'll first create a function getComicsById using Axios to make a GET request.&lt;/p&gt;

&lt;p&gt;This function retrieves comic data based on the provided comicId. We'll also define a custom hook useComicsById that utilizes React Query's useQuery hook.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import axios from "axios";
import { base_url, key, hash } from "./..";
import { useQuery } from "react-query";

async function getComicsById(comicId: number) {
  const { data } = await axios.get(`${base_url}/comics/${comicId}`, {
    params: {
      ts: "1",
      apikey: key,
      hash: hash,
    },
  });
  return data;
}

export function useComicsById(comicId: number) {
  return useQuery(["comic by id", comicId], () =&amp;gt; getComicsById(comicId));
};

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

&lt;/div&gt;



&lt;p&gt;In the code above, we import Axios and necessary configuration variables. The getComicsById function performs the API call using axios.get() and returns the retrieved data. It includes the necessary parameters such as ts, apikey, and hash required by the Marvel API.&lt;/p&gt;

&lt;p&gt;The useComicsById hook utilizes React Query's useQuery hook to handle the data fetching. &lt;/p&gt;

&lt;p&gt;It takes comicId as an argument and uses it to generate a unique query key. The second argument of useQuery is an asynchronous function that calls getComicsById with the provided comicId. &lt;/p&gt;

&lt;p&gt;React Query manages the caching, automatic refetching, and error handling for us.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rendering the Fetched Data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To render the fetched data, we import the useComicsById hook into the ComicDetailsList component. In this component, we use &lt;a href="https://nextjs.org/docs/pages/api-reference/functions/use-router"&gt;Next.js &lt;/a&gt; useRouter hook to obtain the comicId from the URL. &lt;/p&gt;

&lt;p&gt;We then use the useComicsById hook to fetch the data based on the comicId.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useRouter } from "next/router";
import { useComicsById } from "@features/comics/api";
import { ComicDetailsById } from "./..";


export function ComicDetailsList() {
  const router = useRouter();
  const { comicId } = router.query;
  const { data, isLoading, isError } =useComicsById(Number(comicId));
  const { results } = data?.data || {};


  if (isLoading) //...
  if (isError)//...


  return (
    &amp;lt;&amp;gt;
      {results?.map((comic: any) =&amp;gt; (
        &amp;lt;ComicDetailsById
          key={comic.id}
          {...comic}

        /&amp;gt;
      ))}

      //rest of the code...
    &amp;lt;/&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the ComicDetailsList component, we destructure the data, isLoading, and isError properties from the result of the useComicsById hook. We also destructure the results from the data object.&lt;/p&gt;

&lt;p&gt;Based on the loading and error states, we conditionally render different components. &lt;/p&gt;

&lt;p&gt;Once the data is successfully fetched, we iterate over the results array using the map() function and render a ComicDetailsById component for each comic. &lt;/p&gt;

&lt;p&gt;We pass the necessary props to the ComicDetailsById component, such as the key and the spread operator ({...comic}) to pass all the properties of the comic as individual props.&lt;/p&gt;

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

&lt;p&gt;In this article, we learned how to use React Query to fetch data from the Marvel API in a React application. We refactored the code to utilize React Query's useQuery hook for data fetching and caching. By separating the data fetching logic into a custom hook, we kept the component clean and focused on rendering the data.&lt;/p&gt;

&lt;p&gt;React Query provides additional features like caching, automatic refetching, and error handling out of the box, making it a powerful tool for handling data in React applications. Consider using React Query for your data fetching needs to improve the performance and maintainability of your React components.&lt;/p&gt;

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

&lt;p&gt;Hope this helps!&lt;/p&gt;

</description>
      <category>react</category>
      <category>data</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How To Refactor Navigation Links Using map() in React</title>
      <dc:creator>Cath Leyson</dc:creator>
      <pubDate>Mon, 29 May 2023 04:43:39 +0000</pubDate>
      <link>https://dev.to/cathleys/how-to-refactor-navigation-links-using-map-in-react-9m3</link>
      <guid>https://dev.to/cathleys/how-to-refactor-navigation-links-using-map-in-react-9m3</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;In React applications, it's common to have navigation menus with links to different pages. &lt;/p&gt;

&lt;p&gt;Initially, these links might be hardcoded individually, which can be cumbersome and less maintainable as the number of links grows.&lt;/p&gt;

&lt;p&gt;In this article, we'll explore how to refactor hardcoded navigation links using the map() function in JavaScript. &lt;/p&gt;

&lt;p&gt;By leveraging the power of map(), we can simplify our code and make it more scalable and reusable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Before we proceed, make sure you have a basic understanding of React and JavaScript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Refactoring the Navigation Links&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Let's start by refactoring the hardcoded navigation links in the LandingPage. &lt;/p&gt;

&lt;p&gt;Here is the old way I did it&lt;br&gt;
&lt;/p&gt;

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

     return(
        &amp;lt;Nav &amp;gt;
          &amp;lt;LinkItems href="/"&amp;gt;Home&amp;lt;/LinkItems&amp;gt;
          &amp;lt;LinkItems href="/products"&amp;gt;Products&amp;lt;/LinkItems&amp;gt;
          &amp;lt;LinkItems href="/documentation"&amp;gt;Documentation&amp;lt;/LinkItems&amp;gt;
          &amp;lt;LinkItems href="/pricing"&amp;gt;Pricing&amp;lt;/LinkItems&amp;gt;
      &amp;lt;/Nav&amp;gt;

//Rest of the code...
)}

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

&lt;/div&gt;



&lt;p&gt;First, I stored the paths in a file called routes.tsx, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const Routes = { 
home: "/", 
products: "/products",
documentation: "/documentation", 
pricing: "/pricing",
 };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then import the Routes to LandingPage and pass it in the href key in navLinks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Routes } from "@config/routes";
import styled from "styled-components";

//styled-component for anchor tag
const LinkItems = styled.a`
  display: flex;
  text-decoration: none;
  //...
`;

const navLinks = [
  { id: 1, label: "Home", href: Routes.home },
  { id: 2, label: "Products", href: Routes.products },
  { id: 3, label: "Documentation", href: Routes.documentation },
  { id: 4, label: "Pricing", href: Routes.pricing },
];

const LandingPage = () =&amp;gt; {

     return( 
        &amp;lt;Nav&amp;gt;
          {navLinks?.map((link) =&amp;gt; (
            &amp;lt;LinkItems key={link.id} href={link.href}&amp;gt;
              {link.label}
            &amp;lt;/LinkItems&amp;gt;
          ))}
        &amp;lt;/Nav&amp;gt;
//...
)}
export default LandingPage;

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

&lt;/div&gt;



&lt;p&gt;In the code above, we create an array called navLinks that contains objects representing each navigation link.&lt;/p&gt;

&lt;p&gt;Each object has properties like id, label, and href, which correspond to the unique identifier, display label, and URL of the link, respectively.&lt;/p&gt;

&lt;p&gt;Inside the LandingPage, we use the map() function on the navLinks array to iterate over each link object. &lt;/p&gt;

&lt;p&gt;For each iteration, we render a LinkItems component with the key set to the id and the href set to the href property of the link object. The label property is used as the content of the link.&lt;/p&gt;

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

&lt;p&gt;By refactoring the navigation links using map(), we achieve several benefits. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;First&lt;/em&gt;&lt;/strong&gt;, it simplifies the code by reducing duplication. Instead of repeating similar code for each link, we can define the links in a single array and iterate over it. &lt;/p&gt;

&lt;p&gt;This makes the code more readable and easier to maintain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Second&lt;/em&gt;&lt;/strong&gt;, it enhances scalability and reusability. &lt;/p&gt;

&lt;p&gt;Adding or modifying links becomes as simple as adding or modifying objects in the navLinks array. &lt;/p&gt;

&lt;p&gt;The navigation menu can easily accommodate new links without requiring changes to the rendering logic. &lt;/p&gt;

&lt;p&gt;This makes it convenient to scale the navigation menu as your application evolves.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Additionally&lt;/em&gt;&lt;/strong&gt;, separating the links into a separate configuration file (Routes in this case) allows for better organization and separation of concerns. &lt;/p&gt;

&lt;p&gt;It centralizes the management of routes, making it easier to update URLs or handle dynamic routing scenarios.&lt;/p&gt;

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

&lt;p&gt;In this article, we explored how to refactor hardcoded navigation links using the map() function in React. &lt;/p&gt;

&lt;p&gt;By storing the links in an array of objects, we simplified the code, improved scalability, and made it easier to maintain and update the navigation menu. Leveraging the power of map(), we achieved a more reusable and flexible solution.&lt;/p&gt;

&lt;p&gt;Remember to apply this refactoring technique whenever you encounter repetitive code for navigation links or similar scenarios in your React applications. &lt;/p&gt;

&lt;p&gt;Hope this helps!&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How to Test Error Page using Cypress</title>
      <dc:creator>Cath Leyson</dc:creator>
      <pubDate>Mon, 29 May 2023 01:06:17 +0000</pubDate>
      <link>https://dev.to/cathleys/how-to-test-error-page-using-cypress-58pg</link>
      <guid>https://dev.to/cathleys/how-to-test-error-page-using-cypress-58pg</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Cypress is a powerful end-to-end testing framework that allows developers to write tests for web applications. &lt;/p&gt;

&lt;p&gt;In this article, we'll focus on testing the ErrorPage component, which is displayed when there's an error loading project data in our application. &lt;/p&gt;

&lt;p&gt;We'll use Cypress to simulate an error and verify that the ErrorPage is rendered correctly with the expected error message and a refresh button.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before we proceed, make sure you have Cypress installed in your project. If not, you can install it by running the following command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install cypress --save-dev&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting Up the Test&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;To get started, create a new Cypress test file. Me? I'll just name it as error-page.spec.ts. Open the file and write the test case:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;describe("Error Page", () =&amp;gt; {
  context("Error", () =&amp;gt; {
    const ErrorMsg = "There was a problem while loading the project data";

    it("simulates an error when dashboard cannot be displayed", () =&amp;gt; {
      cy.intercept(
        {
          method: "GET",
          url: "https://myURL",
        },
        {
          forceNetworkError: true,
        }
      ).as("getNetworkFailure");

      cy.visit("http://localhost:3000/dashboard");
      cy.wait("@getNetworkFailure");
      cy.wait(10000);
      cy.contains(ErrorMsg).should("be.visible");
    });
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, we intercept the network request to &lt;a href="https://myURL"&gt;https://myURL&lt;/a&gt; endpoint and force a network error. &lt;/p&gt;

&lt;p&gt;This simulates the scenario where the dashboard cannot be displayed due to an error. &lt;/p&gt;

&lt;p&gt;We then visit the dashboard page, wait for the network failure using cy.wait("@getNetworkFailure"), and wait for an additional 10 seconds to ensure the error message is rendered.&lt;/p&gt;

&lt;p&gt;Finally, we use cy.contains() to assert that the ErrorMsg is visible on the page. This verifies that the ErrorPage component is rendered correctly when an error occurs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What Does ErrorPage Component Look Like&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The ErrorPage component is responsible for displaying the error message and providing a refresh button. Here's an example of how the component can be implemented:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useRouter } from "next/router";
import * as E from "./ErrorPage.styles";

export function ErrorPage() {
  const router = useRouter();
  const ErrorMsg = "There was a problem while loading the project data";

  return (
    &amp;lt;E.Container&amp;gt;
      &amp;lt;E.Wrapper&amp;gt;
        &amp;lt;E.ErrorText&amp;gt;{ErrorMsg}&amp;lt;/E.ErrorText&amp;gt;
        &amp;lt;E.RefreshButton onClick={() =&amp;gt; router.reload()}&amp;gt;
          Try again
          &amp;lt;E.ArrowIcon src={"/icons/refresh-arrow.svg"} alt="arrow" /&amp;gt;
        &amp;lt;/E.RefreshButton&amp;gt;
      &amp;lt;/E.Wrapper&amp;gt;
    &amp;lt;/E.Container&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;In the code above, I import the necessary styles from ErrorPage.styles and define the ErrorPage component. It renders the error message using  and provides a refresh button using . Clicking the refresh button triggers the router.reload() function, which reloads the page and attempts to retrieve the project data again.&lt;/p&gt;

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

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

&lt;p&gt;In this article, we've covered how to test the ErrorPage component using Cypress. &lt;/p&gt;

&lt;p&gt;By simulating a network error and verifying that the error message is displayed correctly, we ensure that our application handles errors gracefully.&lt;/p&gt;

&lt;p&gt;By thoroughly testing our error handling components like the ErrorPage, we can improve the overall reliability and user experience of our application.&lt;/p&gt;

&lt;p&gt;Do you have comments or suggestions about this article? please I'm open to any of them!&lt;/p&gt;

&lt;p&gt;Hope this helps!&lt;/p&gt;

</description>
      <category>cypress</category>
      <category>testing</category>
      <category>react</category>
    </item>
    <item>
      <title>How to show/hide Modal in React</title>
      <dc:creator>Cath Leyson</dc:creator>
      <pubDate>Sun, 28 May 2023 04:52:55 +0000</pubDate>
      <link>https://dev.to/cathleys/how-to-showhide-modal-in-react-46fg</link>
      <guid>https://dev.to/cathleys/how-to-showhide-modal-in-react-46fg</guid>
      <description>&lt;p&gt;Modals are a popular UI component used to display important information or capture user input in a non-intrusive manner. In this article, we'll explore how to create a modal in React using the powerful styled-components library. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;: Before we dive into the implementation, make sure you have a basic understanding of React and styled-components. Familiarity with JavaScript and JSX syntax will also be helpful.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting Up the Project&lt;/strong&gt;: To begin, create a new React project using your preferred setup. Once your project is set up, install the necessary dependencies by running the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install styled-components
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Styling the Modal&lt;/strong&gt;: To style the modal, we leverage the power of styled-components. Take a look at it used in the code snippet below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import styled from "styled-components";

const Overlay = styled.div`
  box-sizing: border-box;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: rgba(52, 64, 84, 0.6);
  backdrop-filter: blur(8px);
  animation: fadein 0.5s;

  @keyframes fadein {
    from {
      opacity: 0;
    }
    to {
      opacity: 1;
    }
  }
`;

const Modal = styled.div`
  position: absolute;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 25rem;
  height: 18rem;
  background: white;
  border-radius: 0.75rem;
  box-shadow: 0px 20px 24px -4px rgba(16, 24, 40, 0.1),
    0px 8px 8px -4px rgba(16, 24, 40, 0.04);
  transition: all 0.5s ease;
  z-index: 1;
`;

//The rest of elements content inside the Modal
const Icon = styled.img`
  // Icon styles 
`;

const Title = styled.div`
  //Title styles 
`;

const Text = styled.div`
  // Text styles 
`;

const Wrapper = styled.div`
  // Wrapper styles 
`;

const CancelButton = styled.button`
  // Cancel button styles 
`;

const ContactButton = styled.button`
  // Contact button styles 
`;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By applying styles to each component, we can easily customize their appearance. I won't populate the article with stylings of other elements. The Overlay, Modal and CancelButton though, are what we need here.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementing the ContactModal Component&lt;/strong&gt;:&lt;br&gt;
To create the functional component, Let's take a closer look at the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//typescript value
type ModalProps = {
  setShowModal: React.Dispatch&amp;lt;React.SetStateAction&amp;lt;boolean&amp;gt;&amp;gt;;
};

export function ContactModal({ setShowModal }: ModalProps) {
  return (
    &amp;lt;Overlay onClick={() =&amp;gt; setShowModal(false)}&amp;gt;
      &amp;lt;Modal onClick={(e) =&amp;gt; e.stopPropagation()}&amp;gt;
        {/* Modal Content */}
          &amp;lt;CancelButton
            onClick={() =&amp;gt; setShowModal(false)}
          &amp;gt;
            Cancel
          &amp;lt;/CancelButton&amp;gt;
      &amp;lt;/Modal&amp;gt;
    &amp;lt;/Overlay&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;In the code above, we define the ContactModal component. The Overlay component represents the backdrop of the modal, while the Modal component encapsulates the actual content. By using the onClick event handlers, we can control the visibility of the modal by toggling the showModal state.&lt;/p&gt;

&lt;p&gt;We put stopPropagation() in Modal component's onClick function so that when we clicked it, it prevents from closing the modal pop up.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Triggering the Modal&lt;/strong&gt;: To trigger the modal, we integrate it into our LandingPage . Here's an example of how it can be done:&lt;br&gt;
&lt;/p&gt;

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

const LandingPage = () =&amp;gt; {
  const [showModal, setShowModal] = useState(false);

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;ContactButton
        onClick={() =&amp;gt; setShowModal(!showModal)}
      &amp;gt;
        {/* Contact button icon */}
      &amp;lt;/ContactButton&amp;gt;
      {showModal &amp;amp;&amp;amp; &amp;lt;ContactModal setShowModal={setShowModal} /&amp;gt;}
    &amp;lt;/div&amp;gt;
  );
};

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

&lt;/div&gt;



&lt;p&gt;In the LandingPage, we use the showModal state and the setShowModal function from the useState hook to control the visibility of the modal. When the contact button is clicked, the showModal state is toggled, rendering or hiding the ContactModal component accordingly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn3lxp41vuxjo118j7r49.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn3lxp41vuxjo118j7r49.gif" alt="Image description" width="500" height="430"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Remember, modals are versatile components that can be customized further based on your specific requirements. You can add additional functionality such as animations, form inputs, or dynamic content to enhance the user experience.&lt;/p&gt;

&lt;p&gt;By mastering the creation of modals and utilizing libraries like styled-components, you can elevate the visual appeal and user interaction of your React applications.&lt;/p&gt;

&lt;p&gt;Do you have comments or suggestions about this article, please I’m open to any of them.Thank you!&lt;/p&gt;

&lt;p&gt;Hope this helps!&lt;/p&gt;

&lt;p&gt;Warmly,&lt;br&gt;
Cath&lt;/p&gt;

</description>
      <category>react</category>
      <category>styledcomponents</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
