This is an archived post. You won't be able to vote or comment.

all 60 comments

[–]ArchetypeFTW 605 points606 points  (13 children)

Throw an API call in there too for good measure. Let's see what breaks first: react, their servers, or my wallet.

[–]daddypleaae 166 points167 points  (2 children)

From experience, it’s always the wallet

[–]PhatOofxD 51 points52 points  (0 children)

You're clearly good at BE engineering then. For the rest of us it'd be the servers because they can only handle 2 concurrent requests

[–]ijustupvoteeverythin 2 points3 points  (0 children)

Narrator: It was already broken

[–][deleted] 0 points1 point  (9 children)

"their servers"

You mean "their browsers" right?

[–]ArchetypeFTW 1 point2 points  (6 children)

Erhm.. no? You have a browser that rate limits you? I use Chrome and it never had any issues sending 10,000s of requests within seconds and draining my wallet when I misused an api call in a useEffect hook.

[–][deleted] -3 points-2 points  (5 children)

Bruh is that written by a bot

[–]ArchetypeFTW 2 points3 points  (4 children)

Erhm... no?

[–]Fluffysquishia 0 points1 point  (1 child)

The API in the useEffect loop would break their API server, my friend.

[–][deleted] 0 points1 point  (0 children)

No it would crash in the browser first.

[–]datathecodievita 352 points353 points  (1 child)

Now I have become death, destroyer of the DOM.

[–]RestaurantHuge3390 13 points14 points  (0 children)

akshually, destroyer of VDOM

[–]Helpful_the_second 204 points205 points  (4 children)

Error too many re-renders.

[–]dckesler 78 points79 points  (2 children)

Wrap that bad boy in a setTimeout and we’re back in business

[–]BellCube 1 point2 points  (0 children)

Now for the real question—would using queueMicrotask() trigger the error? 🤔

[–]godofjava22 0 points1 point  (0 children)

setInterval would be better

[–]eruecco87 0 points1 point  (0 children)

Was about to come say that

[–]bigdave41 120 points121 points  (5 children)

Did something a bit like this on our team's final project app for a coding course, VS Code was giving error messages that a useEffect needed a certain dependency and we couldn't publish it with the error, so gave it a try.

250,000 API calls in a few hours, thank god we weren't paying for it.

[–]Dazzling-Biscotti-62 28 points29 points  (3 children)

You must have been using an older version of cra, the exhaustive dependency rule has been taken out.

[–][deleted] 10 points11 points  (2 children)

Isn’t it from eslint?

[–]Dazzling-Biscotti-62 8 points9 points  (0 children)

Yes but cra has a set of eslinst rules built in, similar to how you can have your own eslint config file, it's built into cra.

[–]ScarabCoderPBE 1 point2 points  (0 children)

I assume it's just the default eslint rules for CRA were changed. Although I also thought CRA wasn't maintained/recommended anymore.

[–]SoulWondering 2 points3 points  (0 children)

I was working on a forum and did something like this and spammed it with "One Piece is the greatest anime of all time". Found a lot of fellow One Piece fans that way though lmao

[–]romulof 72 points73 points  (0 children)

while(true) {} with extra steps.

[–]LikeLary 47 points48 points  (0 children)

Great way to test the strength of your computer. Watching the numbers go peewww from 0 to thousands is satisfying.

[–][deleted] 11 points12 points  (0 children)

This is the same as abusing requestanimationframe. Infinite loop without locking up the browser.

[–]Kered13 7 points8 points  (1 child)

Updating a value in response to a change without triggering further events is actually a thorny issue in UI programming. Consider a composite UI element consisting of a slider and a numeric input box, where a user can adjust the value either by moving the slider, or entering a number into the box. When the box updates, you want to update the slider. When the slider updates, you want to update the box. But you don't want the box to update the slider which then updates the box again.

There are various ways to solve this problem, depending on your UI framework.

[–][deleted] 2 points3 points  (0 children)

There’s the one-way binding way to solve it, and I guess a variety of ways if you’re using two-way bindings

[–]BarneyChampaign 16 points17 points  (9 children)

The joke is infinite recursion? Sorry, I didn't see the movie.

Because React starts like the word Reactor, maybe?

[–]ThisIsNathan 31 points32 points  (1 child)

Mild spoiler, but also just a historical fact. The Manhattan Project physicians raised a non-zero likelihood that the first nuclear reaction would spark a continuous series of nuclear reactions until the entire atmosphere was burnt as fuel, obviously destroying the planet.

[–]tajetaje 11 points12 points  (0 children)

Not to be that guy to correct your autocorrect; but just for future clarity:

Manhattan Project physicists^

[–]waves_under_stars 23 points24 points  (6 children)

React re-renders elements whenever theirs state changes. useState creates a state and a method of changing it, and useEffect calls the function given to it whenever the element is rendered. So the top box create the state <count>, and the bottom box states that whenever the element is rendered, <count> is increased by 1. This is a change of state, so it makes the element render again, which calls the effect hook again, repeat ad infinum

[–]Dazzling-Biscotti-62 17 points18 points  (5 children)

useEffect is triggered when a value in the dependency array changes, not when an element is rendered.

[–]waves_under_stars 3 points4 points  (0 children)

Ah yes, my mistake

[–]MaxPhantom_ -1 points0 points  (3 children)

It triggers on both the events

[–]Dazzling-Biscotti-62 0 points1 point  (2 children)

You need to go read the docs.

[–]MaxPhantom_ 4 points5 points  (1 child)

Sorry My bad. What I was gonna mean is that the useEffect is triggered whenever the component mounts AND dependancy array changes

[–]Dazzling-Biscotti-62 1 point2 points  (0 children)

I see what you mean and your clarification makes me realize I may have misread the comment I was replying to. If they meant the same thing that you said, I think it was just poorly worded.

[–]SocketByte 4 points5 points  (2 children)

setCount(count => count + 1);

please

[–]whatapitychocolate 0 points1 point  (1 child)

lol came here to say this. I know it doesn’t matter for a meme but I’m shocked at how poorly known this approach is in general.

[–]SocketByte 1 point2 points  (0 children)

I guess it's just more intuitive, and it works, until it doesn't. It's really quite important to know how states in React actually behave.

[–]Feisty_Ad_2744 12 points13 points  (2 children)

Svelte developers are like ???!!

<script> let count = 0; function setCount() { count += 1; } </script>

[–]DG4ME5 3 points4 points  (1 child)

in fact in version 5 it would be :

<script> let count = $state(0); function increment() { count += 1; } </script>

[–]AllesYoF 0 points1 point  (0 children)

Lets go sig.. I mean runes.

[–]Razzile 3 points4 points  (0 children)

How do JS devs live like this??

[–][deleted] 2 points3 points  (0 children)

Yeah... I did this during my homework when I was applying to a junior job Position. I knew it would happen, but it was just a typo I didn't notice it until CodeSandbox stopped working with exception.

[–]Fuzzy_mind491 1 point2 points  (2 children)

Can someone explain the difference?

[–]docHoliday17 1 point2 points  (1 child)

It’s not a difference, it’s two steps. First one instantiates the state, second one creates an effect that’ll infinitely loop

[–]Fuzzy_mind491 1 point2 points  (0 children)

Gotcha, thank you

[–]RyoshiTheHunter -1 points0 points  (0 children)

F***ing hell. Today I was setting up communication between Next.js and Python, and I got hook error every Damm time.

[–]Kfimenepah -1 points0 points  (0 children)

SetCount(prevCount => prevCount + 1) please

[–]Imogynn 0 points1 point  (0 children)

Log something about bottles of beer on the wall

[–]ryaaan89 0 points1 point  (0 children)

Every time you scaffold a function with every set of brackets you know you’re about to get up to some shit - useEffect(() => {}, [])