Day 4 ReactJS Adventures + Distriator Progress: useState, useEffect, and .env Chaos πΉ
hive-139531Β·@sagarkothari88Β·
31.489 HBDDay 4 ReactJS Adventures + Distriator Progress: useState, useEffect, and .env Chaos πΉ
## π Hello Hive Community! Another day, another ReactJS rollercoaster! π’ We've been deep-diving into ReactJS while pushing progress on **Project Distriator** and **CheckInWithXYZ** πͺ Letβs rewind βͺ what weβve covered so far: - [π **Day One**](https://peakd.com/hive-139531/@sagarkothari88/integrate-aioha-with-your-react-app-from-scratch-vite--tailwind--daisyui): Fresh React app + AIOHA integration! - [π§ **Day Two**](https://peakd.com/hive-139531/@sagarkothari88/integrating-react-router-my-routing-adventures-with-react): Routing drama & rebellious NavBar π€ - [π οΈ **Day Three**](https://peakd.com/hive-139531/@sagarkothari88/from-navbar-mess-to-react-router-success-fixing-layouts-routing-and-aioha-in-one-go): Fixed Layouts, Routing & AIOHA π₯ - **Today is Day Four** π --- ## π― **Today's Goals** 1. Learn to integrate `.env` in React project π 2. Implement first `useState` ποΈ 3. Try out `useEffect` (and totally freak out a little) π΅ 4. Share Distriator project updates π --- ## π **How to Load `.env` Values in React (Vite-based)**  If you're using **Vite + React** like us, it's super simple π ποΈ **Step 1:** Create a `.env` file π Donβt forget to put it inside `.gitignore` β¨ Prefix all variables with `VITE_`, like: ``` VITE_API_KEY=some_key_here ``` π Access it in your app like this: ```js import.meta.env.VITE_API_KEY ``` --- ## π **How to Use `useState`**  `useState` lets you track and update values inside functional components π¦ β **Step 1:** Import it ```js import { useState } from "react"; ``` β **Step 2:** Declare a variable ```js const [businesses, setBusinesses] = useState([]); ``` In **Distriator**, we're tracking a list of businesses β so we start with an empty array. - `businesses`: current value - `setBusinesses`: to update the state Simple & elegant π --- ## π **How to Use `useEffect` (AKA: βWhy is my API Calling TWICE?!β)**  Okay... so this one had me like π΅βπ« I initially wrote: ```js useEffect(() => { const fetchedData = async () => { // some logic to fetch businesses } fetchedData(); }); ``` π£ **Result:** Infinite reload loop! App was like: "I LIVE TO FETCH!" I panicked, closed the app, went into full bug-hunting mode π΅οΈ ### π§ The Fix? I forgot the **dependency array `[]`** ```js useEffect(() => { const fetchedData = async () => { // some logic to fetch businesses } fetchedData(); }, []); // Important! ``` But even then... API was hit **twice** π   π― **Removing `StrictMode` is a fix...** But letβs be real β not the best one!  Turns out, the smarter way is to use **AbortController** to clean up async functions. And yeah... itβs a bit weird. π  Letβs move on before I rage-quit π --- ## π¨ **Distriator Project Updates** Big win on Distriator today! π Weβve successfully integrated **all rooms of Ati Hotel** ποΈ Deployment is live at: **[alpha.distriator.com](https://alpha.distriator.com)** π  βοΈ Screenshot above highlights just **one fileβs worth** of changes, but the impact is massive! Tagging @ecoinstant & @starkerz so you guys can start playing around on the alpha site! --- ## π Thanks for Following Along! Iβll be back soon with more learnings, bugs, laughs & updates. Until then β keep building, keep shipping! π»β¨ **See you in the next post!** π --- ### π Final Note - I asked ChatGPT/AI to help optimize this post to make it more readable and viewer-friendly. - Here is the link where you can find both original content & improvements made by AI - https://chatgpt.com/share/6888bbac-21dc-8000-ba70-26940b53cfe7 --- ### π My Contributions to β¦οΈ Hive Ecosystem | Contribution | To | Hive | Ecosystem | |--------------|----|------|-----------| | Hive Witness Node | Hive API Node (in progress) | 3Speak Video Encoder Node Operator (highest number of nodes) | 3Speak Mobile App Developer | | 3Speak Podcast App Developer | 3Speak Shorts App Developer | 3Speak Support & Maintenance Team | [Distriator Developer](https://distriator.com/) | | [CheckinWithXYZ](https://checkinwith.xyz/) | [Hive Inbox](https://hive-inbox.the-hive-mobile.app/) | [HiFind](https://hifind.the-hive-mobile.app/) | [Hive Donate App](https://donate.the-hive-mobile.app/) | | Contributed to HiveAuth Mobile App | Ecency β 3Speak Integration | Ecency β InLeo Integration | Ecency β Actifit Integration | | [Hive Stats App](https://stats.the-hive-mobile.app/) | [Vote for Witness App](https://witness.the-hive-mobile.app/) | [HiveFlutterKit](https://hiveflutterkit.sagarkothari88.one/docs/intro) | [New 3Speak App](https://3speak.sagarkothari88.one/#/?tab=home) | --- ### π Support Back β€οΈ **Appreciate my work? Consider supporting @threespeak & @sagarkothari88!** β€οΈ | Vote | For | Witness | |------|-----|---------| | [](https://witness.the-hive-mobile.app/#/witnesses/@sagarkothari88) | [sagarkothari88](https://witness.the-hive-mobile.app/#/witnesses/@sagarkothari88) | @sagarkothari88 | | [](https://witness.the-hive-mobile.app/#/witnesses/@threespeak) | [threespeak](https://witness.the-hive-mobile.app/#/witnesses/@threespeak) | @threespeak | ---
π threespeakvoter, somecallmejake, knot, thelittlebank, coolmole, djblendah, ecoinstats, thecrazygm, accelerator, synergized, thecrazygm.bank, bengy, aakom, a-bot, cedricguillas, stoodmonsters, mimi.ruby, stoodkev, aequi, michupa, fredaig, ifhy, picazzy005, archon-gov, elderdark, tub3r0, beffeater, abreusplinter, kaveira, mangos, steembasicincome, cocaaladioxine, irisworld, pokerarema, pof.archon, sbi2, sbi3, joeyarnoldvn, anikys3reasure, awesomegames007, tdas0, piestrikesback, sbi-tokens, qwertm, hoffmeister84, veryhappyday, tekraze, briefmarken, sbi4, sbi5, abrockman, sbi6, monsterrerentals, noloafing, sneakyninja, netzisde, bububoomt, thedailysneak, ravensavage, michael561, hivekr, sbi7, heutorybr, sbi8, everythingsmgirl, sbi9, emsenn0, sbi10, voxmortis, gamer00, penguinpablo, cryptonized, hive.friends, funnyman, alphacore, hungrybear, jacuzzi, ykretz, ambyr00, kiemis, balvinder294, frieder, ahlawat, ecencypoints, aafeng, ardina, spoondawg, sprunk, appreciator, flemingfarm, fronttowardenemy, detlev, bluemist, lenasveganliving, rakkasan84, sunsea, kkarenmp, danielcarrerag, gaeljosser, bertrayo, ravenmus1c, inciter, manuelmusic, marblely, sadbear, bdvoter, miguelbaez, josehany, alenox, bilpcoinbpc, nerdvana, andrewmusic, discoveringarni, lucianav, gabilan55, noalys, pimpstudio-cash, cielitorojo, elgatoshawua, hexagono6, power-kappe, mariaser, fotomaglys, seryi13, cesarsj5, aprasad2325, eustace-kidd, malhy, mcookies, vickoly, liveofdalla, blackdaisyft, castri-ja, idksamad78699, pinkchic, rubilu, abu78, beauty197, kerokus, dahpilot, egbre, javedkhan1989, les90, aslamrer, bipolar95, thelivingworld, franco10, kamaraa, ecp.voter, ecp.rclease, anhdaden146, noelyss, eolianpariah2, marblesz, ecp.curator, olympicdragon, david.dicotomia, ecp.dump, rmach, cindynancy, thereikiforest, tahastories1, lichtkunstfoto, waiviolabs, catrynart, sofiag, hivelist, itsrich, iptrucs, beerlover, thelogicaldude, vixmemon, nathen007, sevatar, jane1289, aquagelas, driipam, stresskiller, good-karma, esteemapp, esteem.app, ecency, ecency.stats, ecency.waves, hive-14396, sorin.cristescu, sayee, bhattg, blumela, darthsauron, albro, arka1, liquidocelotytt, argo8, oadissin, t0xicgh0st, baibuaza, mango-juice, valerianis, julianhorack, spiritabsolute, aftabirshad, vikbuddy, lisfabian, ecoinstant, voter002, fatman, cryptoknight12, jozefkrichards, onewolfe, hivedrip, msp-makeaminnow, dpend.active, sketching, iviaxpow3r, mxm0unite, thorlock, mighty-thor, splinterwhale, draygyn, neruel, wiseagent, mrcripdoe, deepresearch, szukamnemo, techguard, techcoderx, onelovedtube, hive-134220, silasvogt, gaborockstar, bishoppeter1, happymichael, originalmrspice, mrchef111, gisi, illuminationst8, brettblue, toddmck, melor9, icepee, dlike, cowboysblog, iamangierose, techstyle, aletoalonewolf, gabbyg86, iamleicester, neeqi, crowbarmama, brainpod, smacommunity, st3llar, adventuroussoul, jeronimorubio, raoufwilly, shermanedwards, gray00, amymya, passion-fruit, openmind3000, fortune-master, floatinglin, tonysayers33, aperterikk, kolkamkwan, nastapak, kkingg, nongkamsailucky, nareebun,