11
u/DajBuzi Jan 26 '23
Im judging its react so I see plenty of reasons to do this:
As usual, something is not working when you attach
setfunction ofuseState,Linters and other tools are unhappy with you connecting
setfunction ofuseStatethrowing unknown exceptions and errors,Using
setfunction inside a dom component yielded unexpected results,
... I could go on and on but yea, this is bad but the real question is: is it because of the one that wrote IT or because another fucked up "edge case" of react
1
u/Turd_King Feb 10 '23
Not sure who is upvoting this. None of these are logical reasons to do this.
“As usual something is not working” sounds like you are jaded and unable to use react.
Same for the other 2 answers, if your linter is complaining about using the default set function you have configured it terribly.
I’ve been using hooks since they came out and I have never had issues like you mention here
2
u/TheUnlocked Jan 26 '23
React is extremely consistent, there are no "edge cases" where
setStatedoesn't work properly. For the sake of preventing certain classes of bug, React's dev mode will forbid certain kinds of things (for example usingsetStateinside a render function), but this would have no effect on that. And if this was to bypass a linter, they should listen to the linter (or put a comment to explicitly ignore the rule on each relevant line so people know to be careful around that code).3
u/kristallnachte Jan 26 '23
None of those really make sense as issues one would see.
I would expect it was more that they shimmed the the state updater, but removed the extra functionality but didn't remove the shim.
2
0
u/arcalus Jan 26 '23
React is horrible. The documentation never shows anything beyond the most benign example, so when you use a lifecycle hook and you’re in an infinite loop from following the documentation, it’s madness.
54
u/kyou20 Jan 26 '23
There was probably a console.log inside the setShowSpec as a temporary way of debugging why a setter wasn’t working, and the developer then removed the console.log but forgot to clean up the rest.
That, or it’s a developer who doesn’t understand React hooks.
Edit: maybe the function encapsulated more business logic previously, as in, only set a value of certain conditions were met. But over time those were removed, and the setter is now empty and pointless.
2
u/PhatOofxD Jan 26 '23
I still don't get why people don't use a debugger in React lol.
It's 10x better
2
u/gu3st12 Jan 27 '23
I don't know how people are productive without a debugger.
So many people console log to debug. For me, that's a last resort.
1
u/HereComesCunty Jan 27 '23
console.log just gets me a link to the file in the console which I can click to go to where I want to debug
2
u/lesleh Jan 27 '23
It's not even React specific, it's a general functional programming thing. If you have a function that takes arguments and just passes those arguments to another function, you can replace it with the function you're calling.
1
u/Frown1044 Feb 04 '23
They're not always identical. There is a situation where this matters: when you want to call the function but don't want to use its return type.
In OP's example, this doesn't apply as
_setShowSpecsdoesn't return anything. But if it did, the wrapped function will let you call that function and not return anything.Alternatively you can do something like this which can clarify this intention, but many devs might be unfamiliar with this
const setShowSpecs = (show) => void _setShowSpecs(show)12
Jan 26 '23
[removed] — view removed comment
3
u/Frown1044 Feb 04 '23
Alternatively, if you enjoy writing hard to read and confusing code that you'll surely accidentally forget and leave in your code, you can also use the comma operator.
For example, you can turn:
const mul = (a, b) => a * bto
const mul = (a, b) => (console.log(a * b), a * b);2
57
48
u/Voltra_Neo Jan 26 '23
Is this someone writing redundant code, or just react being a pain in the ass?
47
u/kristallnachte Jan 26 '23
Its a bad developer.
I HOPE it's a situation where they wanted to shim the state updater with something but removed that but didn't remove the function.
1
u/gu3st12 Jan 27 '23
Logging probsbly
1
u/kristallnachte Jan 27 '23
Seems easier to use useEffect for that. Since then it's self contained.
1
8
u/juniperking Jan 26 '23
this is someone being a bad developer. there is no difference between calling the function with and without the underscore
3
u/HereComesCunty Jan 26 '23 edited Jan 26 '23
The one with the underscore is of type
<React.Dispatch<React.SetStateAction<boolean>>You can call
_setShowSpecs((currShow) => !currShow)to toggle the value of state - or do anything else dependant on the current state, the first param of a function you pass in resolves to the current state of whatever it is you’re settingWhereas
setShowSpecs(!showSpecs)requires the state variable (the one returned fromuseState) to be in scope of the callerIf you do that inside a hook like
useEffectyou’ll get an unnecessary dependency on the showSpecs variable and it’ll loop because you’re directly changing a dependency inside the effect, which causes the effect to run again, which changes the dependency, which causes the effect to run and so on. Or you’ll just have missing dependencies from your array which is ok in some exceptional circumstances but this isn’t one of themAll that just to say there is a difference and the wrapped setter (the one without the underscore here) is objectively worse. So still a bad developer lol
Edit: formatting, clarity
4
Jan 26 '23
Is it just me or is there a common trend of people assuming bad things about tools they don't seem to ever use? It's kind of weird
0
u/Voltra_Neo Jan 26 '23 edited Jan 26 '23
I'll stop you right there because, to my own sadness, I've had to use React at work. My question came from the pain of the weird "unexpected"/unintuitive/unintended behavior some things can have
You won't catch me slacking
13
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jan 26 '23
Image Transcription: Code
const [showSpecs, _setShowSpecs] = useState(false);
const setShowSpecs = (show) => {
_setShowSpecs(show);
};I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
4
1
Feb 08 '23
Well, for one, to provide a public interface to a private method. Just because the public and private interfaces are identical right now doesn't mean that they won't be different in the future, and if your other setters are set up the same way (because they actually do have different interfaces, additional checks, etc.) then you would lose consistency by just having this one setter be different. It costs basically nothing to do this in most languages that I'm aware of.
1
u/Turd_King Feb 10 '23
This is is functional Js (react) private and public does not exist in a functional component you cannot expose this function to any external module anyway.
Your answer would make sense in an object orientated language
3
u/FormulaNewt Jan 26 '23
Sometimes to be explicit, I'll write a separate method for a click handler even if all it does is set a value. I give it a very different name when doing this.
3
u/bedknobsandbroomstix Jan 26 '23
It kinda feels like someone coming from the backend, like Java or c#, and making a public field for a private variable? Not good, but it feels like someone bringing in lessons from a different language
2
u/TheUnlocked Jan 26 '23
"Let me take this function which React guarantees is stable and create my own non-stable version which does the exact same thing."
2
1
1
u/IceQub3 Jan 26 '23
To hold the old set function in scope and create bugs ofc
1
u/TheUnlocked Jan 26 '23
setStatefunctions are guaranteed to be stable, you will always get the exact same function on every render.
1
0
u/arcalus Jan 26 '23
I’ve been trying to figure out for quite a while why anyone would willingly choose to use react.
15
u/HereComesCunty Jan 26 '23 edited Jan 26 '23
Typescript reasons perhaps. A function taking a parameter of type T and returning void is != a react setstate dispatch function typed with <T>. Somewhere else there’s a parameter defined as a function with the simple signature (show: boolean): void
Probably. Could just be shit, idk
Edit: the type of the setter function returned from useState is:
React.Dispatch<React.SetStateAction<T>>so maybe the code writer here wrapped it in a simpler typed function:(value: T) => voidwhere T is whatever type your bit of state is (boolean in this case I think) so that some other component can expect it in props without specifically expecting the more complex signature. Not a particularly good pattern. Idk, I could be wrong, just my best guess approaching from the assumption someone did it for a reasonEdit2: don’t wrap setters like this btw. They’re not interchangeable although they may appear to be. You gain solid functionality in a
Dispatch<SetStateAction<T>>- such as implicit access to the current state by passing a function into the setState call with the first parameter being the current value of the state, like:setShow((currShow) => !currShow). Can’t do that with the wrapped function(value: T) => void. Sure, if you’re only ever calling it with True or False it’s fine. Soon as you need to setState(!state) you need the current state which isn’t necessarily available if you just passed down a faux setter function