Cookie Consent by Free Privacy Policy Generator Aktuallisiere deine Cookie Einstellungen ๐Ÿ“Œ React Rendering the component Twice


๐Ÿ“š React Rendering the component Twice


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

Hi Techies

If you are using React, you would definitely be using the useEffect hook right?

What if I told you that the React Renders twice and it's a simple bug ๐Ÿคจ๏ธ?
Yeah that's right it's a very small bug, but yet underlooked, although it's only on our local system and not on the actual production build.
But wait a minute, even if it is just in the dev mode. we use vite or other

tools to keep the app running and reflect the changes live. Also, most of us have turned on the auto-save option as the cherry on top.

This becomes a hectic challenge when we use useEffect to make the
API calls. As some free APIs have a limit of 100 calls a day, so I would recommend turning off the auto-save on your IDE to stay safe.

Now After this small story, what would be a feasible solution you may ask?
I have a very simple yet effective solution for Ya. So great if you are following along. Let's see how to tackle this bully.

First of we would like to have some environment variables. Yeah, this would be beneficial, you could choose to name it environment or mode. Here let's go with the environment

In your .env.local you can have it like

VITE_Environent="DEV"
# if using vite
REACT_APP_Environent="DEV"
# If using create-react-app

Have a file to store your environment variables like this

env.config.ts

const RAPIDAPI_KEY: string = import.meta.env.VITE_RAPIDAPI_KEY;
const RAPIDAPI_HOST: string = import.meta.env.VITE_RapidAPI_Host;
const ENVIRONMENT: string = import.meta.env.VITE_Environent;
const ENVS = {
 RAPIDAPI_KEY: RAPIDAPI_KEY,
 RAPIDAPI_HOST: RAPIDAPI_HOST,
 ENVIRONMENT: ENVIRONMENT, 
};

export default ENVS;

I am exporting it like so, to avoid the typos as only by console logs you can detect them so better to do it here once and import and use them elsewhere. Also, look at the naming convention and replace the VITE_ with REACT_APP_ for a smooth experience.

Now, here comes the juicy part for which you have been reading this blog
the proper way to do it.

main.tsx

import React, { memo } from "react";
import ReactDOM from "react-dom/client";
import { Route, BrowserRouter as Router, Routes } from "react-router-dom";
import App from "./App.tsx";
import "./index.css";
import { Provider } from "react-redux";
import { store } from "./state/store.ts";
import ProductsPage from "./pages/ProductsPage.tsx";
import NotFound from "./pages/NotFound.tsx";
import ENVS from "./env.config.ts";
const renderApp =() => (
 <Provider store={store}>
 <Router>
 <Routes>
 <Route path="/" element={<App />}>
 <Route path="products" element={<ProductsPage />} />
 </Route>
 <Route path="*" element={<NotFound />} />
 </Routes>
 </Router>
 </Provider>
)

console.log("ENVS.ENVIRONMENT", ENVS.ENVIRONMENT)
// Conditionally wrap in React.StrictMode based on the environment
// As it is the one causing this issue
// React.StrictMode 
const app =
 ENVS.ENVIRONMENT !== "DEV" ? (
 <React.StrictMode>{renderApp()}</React.StrictMode>
 ) : (
 renderApp()
 );

ReactDOM.createRoot(document.getElementById("root")!).render(app);

Here we have just a simple little change.
We made an arrow function renderApp which is a simple JSX.element
and conditionally put it inside the <React.StrictMode> if the the Environment is not DEV . It does the job and we get the best of both worlds.

Also if you feel at any moment you need the React.StrictMode , you can
easily do so by just putting the renderApp() in the React.StrictMode .

I know this is going to surely help. Keep coding.

...



๐Ÿ“Œ React Rendering the component Twice


๐Ÿ“ˆ 44.89 Punkte

๐Ÿ“Œ Server-Side Rendering (SSR) vs. Client-Side Rendering (CSR): The Fascinating World of Page Rendering


๐Ÿ“ˆ 39.48 Punkte

๐Ÿ“Œ How to Build a Dynamic Dropdown Component in React โ€“ React Compound Component Pattern Explained


๐Ÿ“ˆ 34.48 Punkte

๐Ÿ“Œ This Week In React #185: React Conf, React Query, refs, Next.js after, mini-react...


๐Ÿ“ˆ 30.43 Punkte

๐Ÿ“Œ This Week In React #185: React Conf, React Query, refs, Next.js after, mini-react...


๐Ÿ“ˆ 30.43 Punkte

๐Ÿ“Œ Understanding the "Element" React Component: A Flexible Approach to Conditional Rendering


๐Ÿ“ˆ 30.4 Punkte

๐Ÿ“Œ Why is my React Component re-rendering?


๐Ÿ“ˆ 30.4 Punkte

๐Ÿ“Œ React 19 introduces Component Rendering with the Magic of "use"!


๐Ÿ“ˆ 30.4 Punkte

๐Ÿ“Œ React Pattern - Build Better Component with Compound component


๐Ÿ“ˆ 26.87 Punkte

๐Ÿ“Œ How to Build a Rating Component with the React Compound Component Pattern


๐Ÿ“ˆ 26.87 Punkte

๐Ÿ“Œ Answer: How I can count how many times my component rendered in a react component


๐Ÿ“ˆ 26.87 Punkte

๐Ÿ“Œ CVE-2023-37908 | XWiki Rendering XHTML Rendering cross site scripting (GHSA-663w-2xp3-5739)


๐Ÿ“ˆ 26.32 Punkte

๐Ÿ“Œ Understanding Server-Side Rendering (SSR) vs. Client-Side Rendering (CSR)


๐Ÿ“ˆ 26.32 Punkte

๐Ÿ“Œ Server-Side Rendering v/s Client-Side Rendering


๐Ÿ“ˆ 26.32 Punkte

๐Ÿ“Œ Client-Side Rendering (CSR) Vs Server-Side Rendering (SSR)


๐Ÿ“ˆ 26.32 Punkte

๐Ÿ“Œ Exploring Next.js: Unraveling the Dynamics of Client-Side Rendering vs. Server-Side Rendering


๐Ÿ“ˆ 26.32 Punkte

๐Ÿ“Œ How To Create Custom Alerts in React Using React-Notifications-Component


๐Ÿ“ˆ 24.85 Punkte

๐Ÿ“Œ Mastering React Component Lifecycle: The Foundation for React Concepts


๐Ÿ“ˆ 24.85 Punkte

๐Ÿ“Œ Optimize React Component Performance with Memoization Using React.memo()


๐Ÿ“ˆ 24.85 Punkte

๐Ÿ“Œ โ€œCan't perform a React state update on an unmounted componentโ€ warning in React 18


๐Ÿ“ˆ 24.85 Punkte

๐Ÿ“Œ This Week In React #127: Nextra, React-Query, React Documentary, Storybook, Remix, Tamagui, Solito, TC39, Rome...


๐Ÿ“ˆ 22.83 Punkte

๐Ÿ“Œ This Week In React #131: useReducer, Controlled Inputs, Async React, DevTools, React-Query, Storybook, Remix, RN , Expo...


๐Ÿ“ˆ 22.83 Punkte

๐Ÿ“Œ This Week In React #139: React.dev, Remix, Server Components, Error Boundary, Wakuwork, React-Native, Bottom Sheet...


๐Ÿ“ˆ 22.83 Punkte

๐Ÿ“Œ This Week In React #142: React-Query, Million, OpenNext, Ariakit, Expo-Image, React-Three-Fiber, TS 5.1, Node.js 20, WebGPU...


๐Ÿ“ˆ 22.83 Punkte

๐Ÿ“Œ This Week In React #146: Concurrency, Server Components, Next.js, React-Query, Remix, Expo Router, Skia, React-Native...


๐Ÿ“ˆ 22.83 Punkte

๐Ÿ“Œ This Week In React #188 : React 19 RC0, Data Fetching, Framer Motion, Compiler, Astro, Zod, Remix, Docusaurus, React-Query...


๐Ÿ“ˆ 22.83 Punkte

๐Ÿ“Œ Handling React OTP Input Auth Web | React Native using react-otp-kit package


๐Ÿ“ˆ 22.83 Punkte

๐Ÿ“Œ Handling React OTP Input Auth Web | React Native using react-otp-kit package


๐Ÿ“ˆ 22.83 Punkte

๐Ÿ“Œ Whatโ€™s New in React 19? React Canaries, Actions, and React Compiler


๐Ÿ“ˆ 22.83 Punkte

๐Ÿ“Œ useRef helps you to avoid re-rendering of the component


๐Ÿ“ˆ 22.79 Punkte

๐Ÿ“Œ Going Over The Speed Limit: Synchronous Rendering in React Native


๐Ÿ“ˆ 20.77 Punkte

๐Ÿ“Œ What is The Difference Between Server Side Rendering (SSR) and React Server Components?


๐Ÿ“ˆ 20.77 Punkte

๐Ÿ“Œ heise+ | JavaScript: Die Rendering-Engines von React, Angular und Ember im Vergleich


๐Ÿ“ˆ 20.77 Punkte

๐Ÿ“Œ How to set focus on an input field after rendering in React


๐Ÿ“ˆ 20.77 Punkte











matomo