|
Another dumb question. I am inheriting an old bad system. A part of the project exposes some secrets and tokens on the clientside. They are just straight up in the code. Obviously thats bad but even putting them in .env files to be loaded at runtime still means somewhere eventually those values are bundled into the bundle. So in my mind there are two better practices here: 1) The location the secrets and tokens access should instead be accessed on the backend and not directly on the frontend, and then it should go through normal auth to get the resource (instead of directly to the resource, independent of user as it currently exists). This way we can use our normal auth methods to ensure access to the external endpoint, and we don't have to worry abotu exposing secrets/tokens. OR: 2) Split the bundle into an auth-ed bundle and an un-authed bundle. In this case the unauthed bundle is really just login, and the auth bundle is the app and contains the bundled secrets/tokens. Still not ideal for a few reasons but at least non-authed users will never be able to gain access to these secrets/tokens without having at least a valid login. Option 1 seems better to me, but I am curious if there are other considerations I should take into account.
|
# ? Feb 26, 2023 19:44 |
|
|
# ? May 15, 2024 22:31 |
|
(1) is typically best practice, yes.
|
# ? Feb 26, 2023 19:59 |
|
One more dumb question. I am using libraries that require sharearraybuffers, which requires me to enable some pretty restrictive cors settings. However our login is through google identity services. Is it possible to have cors set up for just hte login page to be unrestrictive, set the access tokens to local storage (yeah I know this is bad), and then flip back to the main app that now has the access tokens in local storage with the restrictive cors. I hate cors.
|
# ? Mar 5, 2023 05:36 |
|
How you respond to CORS is entirely on you, so yes it’s possible. This allows you to only permit things such as what you need on specific pages (or all pages except specific exceptions).
|
# ? Mar 5, 2023 06:56 |
|
This intro to JS course I'm doing has just introduced the unary plus operator and suggested going through the code we've worked on to change it from Number() to the + to convert because 'it is cleaner'. That lead to a definite bit of denial from me and the Python mindset of 'Explicit is better than implicit.' Surely Number() is more readable and more explicit? What's the advantage of using what to me could easily be mistaken for a seemingly random plus sign that was meant to concatenate or perform addition?
|
# ? Mar 6, 2023 20:19 |
|
wtf I didn't even know that was in js, I come from java and so I'm used to parseInt or Number. I guess I can see the case that you're typing less.
|
# ? Mar 6, 2023 20:36 |
|
I definitely prefer Number, though if you have a string I want turned into a number I’m going to use parseInt
|
# ? Mar 6, 2023 21:12 |
|
fuf posted:I'm using React contexts rather than zustand As my little adventures with React continue, I'm now trying to refactor some stuff to use zustand instead of context providers. One thing I can't figure out is how to replace the parameters that I was passing to the context providers. Like right now I have stuff like: JavaScript code:
If I wanna replace <TagContextProvider> with a zustand "store", then what's a good way to reuse Components but with different state stores or different elements of the same store?
|
# ? Mar 7, 2023 10:57 |
|
Just to update on the above, the issue is covered in this thread: https://github.com/pmndrs/zustand/issues/128 Weirdly, the solution is just to use React context providers to pass in a particular store to a component. So it feels like I've come full circle. If I'm still using context providers then why would I use zustand rather than just storing the state directly in the context like I was before? The motivation for moving to zustand was to try and avoid the issue where components using a context provider have to re-render every time anything in the context changes. I'm assuming that if the context contains a zustand store then all the components are going to re-render when the store changes? I haven't actually tested this so maybe not.
|
# ? Mar 8, 2023 10:27 |
|
fuf posted:So it feels like I've come full circle. If I'm still using context providers then why would I use zustand rather than just storing the state directly in the context like I was before? As long as you're memoizing the newly instantiated zustand store at the Provider level, the reference to the store never changes, so the context won't cause a re-render (unless you redefine/recreate your store) If you have global state which doesn't need multiple instances, then you don't need to use a Context at all, since all consumers can share a direct import reference to the same instance of the store. Ima Computer fucked around with this message at 14:53 on Mar 8, 2023 |
# ? Mar 8, 2023 14:49 |
|
Hmm ok thank you. It might be time to finally learn what memoize means.Ima Computer posted:If you have global state which doesn't need multiple instances, then you don't need to use a Context at all, since all consumers can share a direct import reference to the same instance of the store. yeah I have a handle on this, and zustand is really good for that stuff. But I thought I was being clever by making big chunks of the UI reusable, like the same interface but with different (state) data loaded in. And this is where it gets tricky because it doesn't know which store to use. fuf fucked around with this message at 14:59 on Mar 8, 2023 |
# ? Mar 8, 2023 14:52 |
|
code:
|
# ? Mar 8, 2023 17:51 |
|
LifeLynx posted:Clicking the Search button once searches for the default state set in cardName, which is nothing. Clicking the button again does work though. I seem to recall a way to "warm up" the app using useEffect, but I can't for the life of me figure out what to put there. I also remember reading recently that useEffect was getting phased out as a way to do pretty much everything. What's the answer to get the search to work on the first click? It's because React state variables are a bit tricky and updating them doesn't work how most people would assume. If you want to see what is happening put console.log(e.target[0].value,cardName) just before and after the setState(e.target[0].value) line. Basically the setState doesn't update the state in your functions when it is called, it updates them *after* your functions are completed. They are still using the value of cardName from before the setState was called. There's three ways of fixing your problem: 1) (Probably the standard) Turn your form into a controlled component form. I think you are trying to do this (that's why you have cardName as a state variable), but haven't. Make the value of the input cardName and use an onChange to update cardName when the input field is changed, rather than on submitting the form. 2) Change cardsearch to take the card name as an input and use cardSearch(e.target[0].value). You can possibly get rid of the cardName state variable since it isn't actually needed. 3) Have a useEffect that is depending on cardName that calls cardsearch, probably condtionally on the value of cardSearch to stop it fetching on mount. This is by far the worst. The easiest way to understand useEffect is that it it a bit of code that runs whenever one of its dependencies change in value. A lot of people use it when they don't need to since if your code is the only thing changing the value of a state variable, you can just put the code in a function and call that instead of relying on useEffect (which is slow, can run when you don't expect it and possibly cause infinite loops of variable changes). For whatever reason it also runs once when the component mounts, so it can be used to fetch data on mount. This meant that a lot of people learned useEffect as the magical inital data fetch hook rather than what it is.
|
# ? Mar 8, 2023 19:19 |
|
Soviet Space Dog posted:It's because React state variables are a bit tricky and updating them doesn't work how most people would assume. If you want to see what is happening put console.log(e.target[0].value,cardName) just before and after the setState(e.target[0].value) line. Basically the setState doesn't update the state in your functions when it is called, it updates them *after* your functions are completed. They are still using the value of cardName from before the setState was called. There's three ways of fixing your problem: Thanks, #2 is what I went with. There's eventually going to be more than one input and I don't want to make an API call every time that one input field updates, so I have the whole thing on form submit. #3 is what I learned the last time I was serious about learning React. I probably relied on it too heavily, but I see that was a common mistake!
|
# ? Mar 8, 2023 20:31 |
|
Here's a React state value/reference issue that I don't understand. I have a zustand store that uses immer and looks kind of like: JavaScript code:
How can I make currentTag a reference to an object in the array?
|
# ? Mar 10, 2023 09:56 |
|
When you're coding, how do you decide which version to aim for? This course I'm working through is often explaining the original way to do it, and then how it can now be done. It used to be done by ... but in ES6/ES2020/ES2022 this new functionality was added to make it easier to do. One example being how to get the last element of an array.JavaScript code:
|
# ? Mar 11, 2023 17:05 |
|
In WebPack land it’s almost always been: develop for the latest and transpile to ES5, and possibly ES6 if generous. I’ve been perusing the VSCode base, and there is a lot of ES5-only code due to Electron being clunky. MrMoo fucked around with this message at 17:51 on Mar 11, 2023 |
# ? Mar 11, 2023 17:49 |
|
MrMoo posted:In WebPack land it’s almost always been: develop for the latest and transpile to ES5, and possibly ES6 if generous. Interesting. So does this mean that the main advantage of new features is mainly that they provide a simpler way of accomplishing a similar task rather than adding actual new functionality? This makes it faster for developers but doesn't change things in the backend? Other question... The course I'm doing has some HTML like this... HTML code:
JavaScript code:
But if I change the click event to a mouseenter event, that doesn't trigger the parent nodes to change colour. To me, that makes it seem less like the event is bubbling up, and more like it triggered them all because in clicking on the eg the a tag, I also clicked on its parents too. Whereas mouseentering the a tag didn't see me mouseenter the parents. Am I wrong? Do only certain events propogate/bubble?
|
# ? Mar 11, 2023 19:10 |
|
Yes, only some events bubble. https://developer.mozilla.org/en-US/docs/Web/API/Event/bubbles MDN is a fantastic resource for (web focused) JavaScript
|
# ? Mar 11, 2023 19:49 |
|
fuf posted:Here's a React state value/reference issue that I don't understand. Why do you care about them being the same object in the first place?
|
# ? Mar 11, 2023 21:43 |
|
Sad Panda posted:Interesting. So does this mean that the main advantage of new features is mainly that they provide a simpler way of accomplishing a similar task rather than adding actual new functionality? This makes it faster for developers but doesn't change things in the backend? Anything that’s actually new (rather than just syntactic sugar), will require a polyfill for the older browsers.
|
# ? Mar 11, 2023 22:17 |
|
I was curious whether doing a bunch of drawImage to a single canvas, or having a bunch of canvas objects moving around, would be better for a game-style "redrawing the entire screen every frame" sort of thing, so I made a little test. Turns out drawing 2000 64x64 sprites from off-screen canvases with drawImage runs at 6fps on my machine, versus moving 2000 on-screen canvases with the same content runs at 20fps. (So obviously it would be even more-better to use moving elements if there's some part of the content that doesn't update.) Though now I feel like I should also test how it handles with a 3D context, making the sprites as textures associated with triangle-pairs, and refreshing a vertexbuffer to move them around. But for my purposes I'm pretty sure on-screen canvases is going to be adequate, and so much less annoying to code than the real high performance option. Edit: to be clear, "for my purposes" is a lot fewer than 2000 on-screen items. And probably also interacting with some vector-drawing. roomforthetuna fucked around with this message at 22:49 on Mar 11, 2023 |
# ? Mar 11, 2023 22:47 |
|
Roadie posted:Why do you care about them being the same object in the first place? I have a component for updating a tag that uses currentTag from the zustand store, and also an "updateTag" option that takes a tag ID: JavaScript code:
e: the workaround I came up with is to just store an ID in currentTag instead of an actual tag object, so now my update component looks like: JavaScript code:
fuf fucked around with this message at 12:36 on Mar 13, 2023 |
# ? Mar 13, 2023 12:27 |
|
Seeing NestJS claim the 3rd spot on the NPM javascript frameworks list does nothing but validate my entire theory that Angular's architecture was always the correct way of doing all of this poo poo and that React's popularity isn't due to it being good, but rather popular. This is a controvesial but ultimately correct opinion.
|
# ? Mar 15, 2023 00:41 |
|
no
|
# ? Mar 15, 2023 04:43 |
|
|
# ? Mar 15, 2023 17:44 |
|
Student found something weird that I can't seem to explain to my own satisfaction via looking at the MDN/EMCAScript specification, so I'm not sure if I'm reading the wrong thing or what. Here's some bullshit JavaScript. code:
|
# ? Mar 15, 2023 17:44 |
|
death cob for cutie posted:Student found something weird that I can't seem to explain to my own satisfaction via looking at the MDN/EMCAScript specification, so I'm not sure if I'm reading the wrong thing or what.
|
# ? Mar 15, 2023 17:58 |
|
death cob for cutie posted:Why can I put multiple strings inside the brackets here? Obviously the behavior is that it returns the value associated with the last key (assuming it exists), but why does the interpreter even accept multiple arguments for whatever function is actually called (this is where I think I'm not finding the right thing) when I use square brackets here? You can put any JS expression you want inside a property accessor that uses square brackets syntax. The "evaluates to last value" behavior is just how the comma operator works. Its rules apply any time you shove a comma somewhere you wouldn't normally expect to see one.
|
# ? Mar 15, 2023 18:08 |
|
Ima Computer posted:You can put any JS expression you want inside a property accessor that uses square brackets syntax. oh-ho! much is explained now, thank you
|
# ? Mar 15, 2023 18:13 |
That makes total sense once you know about it, but now I'm just trying to imagine troubleshooting it without that knowledge Or doing that on purpose
|
|
# ? Mar 15, 2023 20:10 |
|
Yeah; my current job is basically writing an introductory programming/JavaScript textbook for my company's part-time students. They don't get as much instructor time as others, and are coming in as complete and total newbies, so a. a lot of things confuse them, b. they don't necessarily have an immediate expert on hand and c. they're not great at reading documentation/Googling code stuff yet. I'm writing a lot of examples of how stuff breaks or otherwise behaves weird based on what students have shown me previously.
|
# ? Mar 15, 2023 20:18 |
|
Data Graham posted:That makes total sense once you know about it, but now I'm just trying to imagine troubleshooting it without that knowledge code:
roomforthetuna fucked around with this message at 01:40 on Mar 16, 2023 |
# ? Mar 16, 2023 00:49 |
|
I think some minifiers use the operator a lot, but yeah I haven't really seen it used for anything other than turning your code into a golf course. e: MDN to the rescue as always MDN - Comma operator (,) posted:This is commonly used to provide multiple updaters to a for loop's afterthought. is that good fucked around with this message at 05:39 on Mar 20, 2023 |
# ? Mar 20, 2023 05:31 |
|
For loops have named parts, and one of them is the "afterthought"? I had no clue.
|
# ? Mar 20, 2023 19:32 |
|
Doom Mathematic posted:For loops have named parts, and one of them is the "afterthought"? I had no clue. I also only recently learned this when I had to start writing some more technical stuff for students.
|
# ? Mar 20, 2023 19:38 |
|
Doom Mathematic posted:For loops have named parts, and one of them is the "afterthought"? I had no clue. Every piece of syntax is going to have a specific name
|
# ? Mar 20, 2023 20:25 |
|
Doom Mathematic posted:For loops have named parts, and one of them is the "afterthought"? I had no clue. In a for loop, there are three conditions - these are referred to as initialization, condition, and afterthought. Do we know who or which standard started calling the third condition "the afterthought"? chatgpt 4 posted:It is difficult to pinpoint a specific individual or standard that started referring to the third part of a for loop as "the afterthought." The term "afterthought" is a colloquialism that has been used by some programmers to describe the third part of a for loop, which is often referred to as the "update" or "increment" statement. This term likely arose informally within the programming community as a way to describe the role of the third part in a for loop, which typically involves updating the loop control variable. That's true, it's not universally recognized. I see it on the mdn website's definition of a for loop - are there any programming language standards that refer to the "update" statement as "afterthought"? chatgpt 4 posted:After searching through various programming language documentation and standards, it appears that none of them officially refer to the "update" statement as "afterthought." MDN (Mozilla Developer Network) is a valuable resource for web developers, but it is not an official language standard. I'm seeing "afterthought" crop up on websites teaching new programmers how to use for loops, and I do see some recent stackoverflow stuff referring to afterthought, but it's not as if this name comes from any official standard.
|
# ? Mar 21, 2023 17:17 |
|
Bruegels Fuckbooks posted:That's true, it's not universally recognized. I see it on the mdn website's definition of a for loop - are there any programming language standards that refer to the "update" statement as "afterthought"? There was a "citation needed" entry on the usage of "afterthought" added back in 2013 ("Who calls it an afterthought?"), but it was removed in a seemingly unrelated update. At this point I'm hoping someone updates the wiki page and adds MDN as the source so we can go full circle. EDIT: odd, I was sure the wayback machine only had snapshots to 2021 when I checked yesterday, but it actually looks like it has two snapshots from around the time "afterthought" was introduced to the wiki page (20th December 2012). 4th of November 2012 26th of December 2012 In both snapshots the article was written back in June 25th, 2007, and has no mention of "afterthought" on the page. fakemirage fucked around with this message at 13:30 on Mar 22, 2023 |
# ? Mar 21, 2023 20:50 |
|
|
# ? May 15, 2024 22:31 |
|
I never even heard of it before this discussion, and it doesn’t even make that much sense. It’s a bit funny, yes, but if I was interviewing someone and they used that, I’d dock a point.
|
# ? Mar 21, 2023 23:01 |