Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
novamute
Jul 5, 2006

o o o
You can also use libraries like nock to mock the actual web request behind the scenes

Adbot
ADBOT LOVES YOU

novamute
Jul 5, 2006

o o o
The other question to ask yourself is "Do I want my co-workers to be the kind of people that agreed to go through this same bullshit?"

novamute
Jul 5, 2006

o o o
Nah that is normal. Union types are better in every way.

novamute
Jul 5, 2006

o o o
If that's a thing you need to do there is a slightly hacky workaround https://twitter.com/housecor/status/1387083288989380610

novamute
Jul 5, 2006

o o o

fsif posted:

And now in React 18 strict mode, they've been forcing useEffect to run twice, which effectively breaks a lot of common implementations of the hook (like data fetching).

So, you know, the React team has kind of been implying that the community has been using useEffect incorrectly for the past four years or whatever and clearly they've been rethinking the paradigm. My assumption is that they are trying to sunset useEffect's role as one of the "main" hooks and relegate to more edge case-y uses.

I was going to say "This should be totally fine as long as you're properly handling the cleanup on the effect" but the fact that almost nobody actually does that seems to be the problem they're trying to address at the framework level.

novamute
Jul 5, 2006

o o o
nx may be an option too

novamute
Jul 5, 2006

o o o

prom candy posted:

After doing some more research I still don't really understand how you're supposed to do safely data fetching in useEffect now. I guess for any heavy computation like that you need to create a ref that says "this already ran so don't run it again?" Also yeah I know I should just use a third party data fetching lib and I normally do.

useMemo is what you should be using to avoid redoing heavy computation rather than useEffect+ref

For safe data fetching the main thing is to just 1) run any cleanup necessary in the useEffect cleanup function and 2) be aware that responses can still come back after the cleanup func depending on how you aborted the initial request (and can arrive out of order coming back even after the newest rendered useEffect fetches successfully).

https://beta.reactjs.org/learn/synchronizing-with-effects#fetching-data

novamute
Jul 5, 2006

o o o

worms butthole guy posted:

So I have a useeffect that fetches a API every 5 seconds and updates data. One thing I'm not particularly good at is remmebering how to only save different data to state. So right now I have


useEffect
Data= Fetch API
SetState(data)
Runeveryfiveseconds


How would I do that setState to examine the data it has and then only add new data it sees?

By comparing the state vs data before calling setState? That's only if you don't want the state to update at all. The main alternative is to just update the state every time and make sure whatever components depend on it to render are properly memoed so they won't rerender if the relevant portions of the state haven't changed.

novamute
Jul 5, 2006

o o o
Can you SSR the page with the API items in it?

novamute
Jul 5, 2006

o o o

The Merkinman posted:

Just so I'm understanding... When back is pressed to instead call a server side rendered version of the page?

Yeah, and if you need direct links to particular items or places in the page you can render them with anchors or just use your scroll position now that the page is static. Whether that's reasonable kind of depends on the size of the response from the API and whether you can work an SSR page into your existing SPA architecture.

novamute
Jul 5, 2006

o o o

Lumpy posted:

Is anyone publishing two or more discreet packages from a single monorepo? If so what are you using, and do you regret doing things this way?

Yes, nx, and no

novamute
Jul 5, 2006

o o o

teen phone cutie posted:

but also, pinning all your versions in package.json is a thing you should more-or-less enforce always

in other words, this:

"react": "16.12.0"

instead of this:

"react": "^16.12.0"

and so on for every other package

Because you don't trust them to follow semver or you don't want to rely on your package managers resolution logic?

Adbot
ADBOT LOVES YOU

novamute
Jul 5, 2006

o o o
IMO React itself is great mostly because it doesn't try to do everything for you however that also means the community solutions for the missing pieces are typically much lower quality in terms of dev experience. Agreed that most of the ecosystem is a mess.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply