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

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.

Adbot
ADBOT LOVES YOU

The Merkinman
Apr 22, 2007

I sell only quality merkins. What is a merkin you ask? Why, it's a wig for your genitals!
We have a website that is broken up into three projects, with a lot of shared stuff between them. Most of time only one of the projects is being worked on at a time, but building in Jenkins builds all 3 so it takes a while. One person had the idea to package each of the 3 in our repository so we could just npm install the ones we aren't working on. Those packages would be updated every time anyone makes a change, even dev-level, not production. I feel like this is just more work than necessary and a better route is to just put the shared stuff in a package and just not navigate to the pages of the projects not being worked on. In the event that something in the shared package is being updated, which isn't nearly as often, then the person would need to build all 3 to test anyway.

Is the other idea actually good? Is there something else to do instead?

prom candy
Dec 16, 2005

Only I may dance

The Merkinman posted:

We have a website that is broken up into three projects, with a lot of shared stuff between them. Most of time only one of the projects is being worked on at a time, but building in Jenkins builds all 3 so it takes a while. One person had the idea to package each of the 3 in our repository so we could just npm install the ones we aren't working on. Those packages would be updated every time anyone makes a change, even dev-level, not production. I feel like this is just more work than necessary and a better route is to just put the shared stuff in a package and just not navigate to the pages of the projects not being worked on. In the event that something in the shared package is being updated, which isn't nearly as often, then the person would need to build all 3 to test anyway.

Is the other idea actually good? Is there something else to do instead?

If the projects are JS have you looked at Turborepo? I don't fully understand the situation you're describing but I think Turborepo is supposed to solve these kinds of issues with its build caching.

The Merkinman
Apr 22, 2007

I sell only quality merkins. What is a merkin you ask? Why, it's a wig for your genitals!
We have a monorepo and I guess trying to reduce build times since it builds all of the projects in the monorepo even if the developer is only working on one.

prom candy
Dec 16, 2005

Only I may dance

The Merkinman posted:

We have a monorepo and I guess trying to reduce build times since it builds all of the projects in the monorepo even if the developer is only working on one.

I think that's exactly what turborepo is supposed to solve since it caches your builds and only rebuilds projects if files have changed. I believe I also saw someone who created a deploy shell script for GitHub actions that would use the output from that build to only deploy projects that had changed. If a project matched "cache hit" in its output then it wouldn't deploy, but if it matched "cache miss" it would deploy.

I'm just in the middle of evaluaing turborepo for our company right now (moving from a many-repo setup) so I can't speak personally to how good it is. Our tooling is a poo poo show at the moment so basically anything looks incredible to me.

teen phone cutie
Jun 18, 2012

last year i rewrote something awful from scratch because i hate myself

The Merkinman posted:

We have a monorepo and I guess trying to reduce build times since it builds all of the projects in the monorepo even if the developer is only working on one.

at my last company we talked about (but never implemented) writing a small bash script that checked changed files against main branch and would conditionally run build commands.

a lot easier said than done, but that's most likely how i would approach it

The Fool
Oct 16, 2003


most cicd platforms can do folder filters so the pipeline only runs when files in that folder have changed

minato
Jun 7, 2004

cutty cain't hang, say 7-up.
Taco Defender
Bazel is also supposed to solve that problem; you configure all the monorepo dependencies, and it knows precisely what to build.

But Bazel is also so insane to configure that it likely causes more problems than it solves.

Buck is Facebooks version of the same thing. I don't know of anyone outside FB who has adopted it.

novamute
Jul 5, 2006

o o o
nx may be an option too

The Merkinman
Apr 22, 2007

I sell only quality merkins. What is a merkin you ask? Why, it's a wig for your genitals!
Thanks for all the suggestions. So it sounds like this is a common situation, but the whole "publish new version to npm on any change" is not the way to address it.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
Are you guys telling me that these webdev build systems don't come out of the box with a way to say "don't build literally everything in the folder tree, just build the stuff that actually goes into the target I'm trying to build"?

Literal make was able to do this forty years ago.

Chenghiz
Feb 14, 2007

WHITE WHALE
HOLY GRAIL

Jabor posted:

Are you guys telling me that these webdev build systems don't come out of the box with a way to say "don't build literally everything in the folder tree, just build the stuff that actually goes into the target I'm trying to build"?

Literal make was able to do this forty years ago.

this isn't the problem that was presented, hth

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

Chenghiz posted:

this isn't the problem that was presented, hth

The Merkinman posted:

We have a monorepo and I guess trying to reduce build times since it builds all of the projects in the monorepo even if the developer is only working on one.

What is the problem being asked about then?

Is it keeping around previous build outputs so that you don't need to rebuild the subprojects you're not actively changing? That's something that forty-year-old software was also able to do, at least on the level of "not rebuilding this entire subproject if nothing in it changed".

minato
Jun 7, 2004

cutty cain't hang, say 7-up.
Taco Defender
Our CICD examines the PR / commit and generates a list of affected files, which it compares with a (hard-coded) dependency graph to determine which build targets need to be rebuilt.

Yeah, make does the same thing but make is typically only aware of files modified locally since the last build. In a CICD env where the repo is pristine make assumes all files are new so it will build everything. To use make effectively in this situation, it'd somehow need to be aware of the list of files changed in the PR. Presumably 40 year old software has git-aware support, but idk, I haven't used make in forever.

prom candy
Dec 16, 2005

Only I may dance
Anyone using Vite? I just dropped it in as a replacement for create-react-app in one of my apps and wow is it ever fast.

The Fool
Oct 16, 2003


I was just introduced to it by a coworker and and we're using it in a poc, seems pretty good so far

ynohtna
Feb 16, 2007

backwoods compatible
Illegal Hen
I use it with Svelte/Svelte-Kit and it is very good compared to the alternatives I've suffered in the past.

My only real gripe is it'll sometimes need restarting if I screw with it's environment (env vars, file names, folders) too much when refactoring. But it restarts really fast!

teen phone cutie
Jun 18, 2012

last year i rewrote something awful from scratch because i hate myself
trying out remix-run and never having tried out next.js, god drat this is a lot of abstracted-away magic.

very nice, but kinda annoying but i guess that's the world we live in now

prom candy
Dec 16, 2005

Only I may dance

teen phone cutie posted:

trying out remix-run and never having tried out next.js, god drat this is a lot of abstracted-away magic.

very nice, but kinda annoying but i guess that's the world we live in now

Which parts? I didn't find it that magical, but then again I'm a recovering Rails developer

teen phone cutie
Jun 18, 2012

last year i rewrote something awful from scratch because i hate myself
i'm pretty early on in my learning but for one, the naming of loader, action, etc functions that just run magically to fetch data

abraham linksys
Sep 6, 2010

:darksouls:
"a library is code you call; a framework is code that calls you" is probably too cheeky to be accurate but also a good way of framing next/remix etc

we've been writing code using frameworks like that for decades, just hasn't been in vogue in javascript lately, instead leaving people to basically write their own frameworks for every non-trivial project (at work we have a horrendous home-rolled data data loading+server-side rendering layer we're trying to just swap out for nextjs getServerSideProps in a new codebase). i'm really excited about this new generation of react frameworks

gbut
Mar 28, 2008

😤I put the UN🇺🇳 in 🎊FUN🎉


abraham linksys posted:

at work we have a horrendous home-rolled data data loading+server-side rendering layer we're trying to just swap out for nextjs getServerSideProps in a new codebase

Is every other company out there doing this poo poo, or are we all working for the same company?

kedo
Nov 27, 2007

I’m going to be jumping into react soon and haven’t used it in years, many versions ago. Does anyone have a recommendation for some good training/courses to get me back up to speed? I’d like something that starts from the ground up so I can unlearn all my old bad habits and get a handle for the right way to use it these days.

N.Z.'s Champion
Jun 8, 2003

Yam Slacker
the new React docs beta site is good

prom candy
Dec 16, 2005

Only I may dance

teen phone cutie posted:

i'm pretty early on in my learning but for one, the naming of loader, action, etc functions that just run magically to fetch data

Ah, yeah I guess I don't find it that magical. No more than getServerSideProps or whatever. Every framework is gonna have some level of convention. Earlier today on our Rails app I couldn't figure out where a method was defined. THAT'S magic, and it sucks.

teen phone cutie
Jun 18, 2012

last year i rewrote something awful from scratch because i hate myself
so actually one thing that i'm not understanding about remix (and i guess SSR in general), how do I go about using component libraries?

Like for example, I want to install this drawer react component, but the issue is it's doesn't handle SSR, so none of the styles the drawer needs show up when the page is rendered. Does this just mean i'm totally hosed and probably going to have to roll my own interactive components? Things that need JS to appear like modals, toasts, tooltips, etc.

N.Z.'s Champion
Jun 8, 2003

Yam Slacker
I don't know about Remix but usually you can indicate that a component should only be used client-side with React.lazy and the import function import('a.tsx').then(module => ...) (not to be confused with the import statement import a.tsx). This might be relevant https://github.com/remix-run/remix/discussions/2936

prom candy
Dec 16, 2005

Only I may dance

teen phone cutie posted:

so actually one thing that i'm not understanding about remix (and i guess SSR in general), how do I go about using component libraries?

Like for example, I want to install this drawer react component, but the issue is it's doesn't handle SSR, so none of the styles the drawer needs show up when the page is rendered. Does this just mean i'm totally hosed and probably going to have to roll my own interactive components? Things that need JS to appear like modals, toasts, tooltips, etc.

How does the drawer normally include its styles? A lot of libraries require you to import a CSS file from the dist folder, which in the case of Remix you would use in conjunction with the links function.

For me when I'm using SSR it's not really with the expectation that things will look correct without javascript, just that the content is on the page. So if it's using CSS-in-JS and looks whack with Javascript turned off my suggestion would be to turn Javascript on :v:

Roadie
Jun 30, 2013

novamute posted:

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.

:yeah:

prom candy
Dec 16, 2005

Only I may dance
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.

teen phone cutie
Jun 18, 2012

last year i rewrote something awful from scratch because i hate myself

prom candy posted:

How does the drawer normally include its styles? A lot of libraries require you to import a CSS file from the dist folder, which in the case of Remix you would use in conjunction with the links function.

For me when I'm using SSR it's not really with the expectation that things will look correct without javascript, just that the content is on the page. So if it's using CSS-in-JS and looks whack with Javascript turned off my suggestion would be to turn Javascript on :v:

ah yes okay that did it :cripes:

i'm getting the feeling i'm about to be asking a lot of stupid questions in this thread

barkbell
Apr 14, 2006

woof

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.

I thought the idea was that's not what they wanted useEffect to be used for

e: i dunno what you are supposed to use tho

barkbell fucked around with this message at 01:26 on Jul 19, 2022

Roadie
Jun 30, 2013

barkbell posted:

I thought the idea was that's not what they wanted useEffect to be used for

e: i dunno what you are supposed to use tho

Use Tanstack Query (formerly react-query). It does all the complicated "doing it right" stuff for you.

prom candy
Dec 16, 2005

Only I may dance

teen phone cutie posted:

ah yes okay that did it :cripes:

i'm getting the feeling i'm about to be asking a lot of stupid questions in this thread

fire away, i'm always happy to be on the answering end of stupid questions on this forum, it's a nice break from being on the asking end.

Roadie posted:

Use Tanstack Query (formerly react-query). It does all the complicated "doing it right" stuff for you.

don't these kinds of libraries all use useEffect under the hood though?

I like that so many libraries are going cross-platform to work with React, Vue, and Svelte. I haven't used react/tanstack-query because I settled on RTK-Query but I'll have to take another look at this since it seems like it's becoming the standard.

Roadie
Jun 30, 2013

prom candy posted:

don't these kinds of libraries all use useEffect under the hood though?

Yes, but they do it correctly.

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

fsif
Jul 18, 2003

I can’t find a straight answer: if I deploy a SSG Next site on Netlify, am I still able to use next/image?

prom candy
Dec 16, 2005

Only I may dance

novamute posted:

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

Thanks, those docs make it pretty clear. Maybe I'll just use Remix for everything from now on.

worms butthole guy
Jan 29, 2021

by Fluffdaddy
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?

Adbot
ADBOT LOVES YOU

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.

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