As I deepen my knowledge of functional programming through functional components and…
Rate quality
Private research annotation
Sign in to add a private research label or note.
- User
As I deepen my knowledge of functional programming through functional components and effects in React, I can't help but be shocked at how it is possible to program this way--how wonderful this new paradigm shift is--and how it is both elegant and, to the uninitiated and even at first to myself (having been trained as an object-oriented programmer back in 2011, when I learned my first object-oriented programming language, Java, at 18 years old, I saw the wonderful advantages of that new paradigm shift, and even had experiences with an earlier exciting new paradigm for me at age 13 when I started using structured programming instead of gotos and lables at 13 in TI-BASIC) somewhat difficult to believe that it is ultimately a simpler and more elegant way. It is now of course 2024, and I recently graduated with a degree in Software Development. I used React in university and could appreciate the simplicity of combining JavaScript code within HTML components that simply returned the HTML and code as a function. It made me realize that it wasn't necessary to have the ability to mutate state directly everywhere in your code. A lot of code just doesn't need it. Now that I'm diving even deeper into React, being charged with the task of building a front-end for a startup, I'm just shocked at what can be done with functional-style programming. Just as an example, I made an effect called useIntersectionRatio (a useEffect hook, which I've simply put into its own file). This allows me to utilize the Intersection Observer API to dynamically keep track of how much one of my elements is overlapping the viewport, so I can tell if it is becoming partially visible and needs to be repositioned to make it fully visible again. But what astonishes me about this is that is (a) my code's side effect, the need to keep track of this state, and the logic that deals with that side effect, can be packed away and completely separated from the functional component by packing it into this useIntersectionRatio.tsx file, and (b) that a functional component, which is just a function--really, that is all that it is--can through successive function calls, manage to maintain a variable somewhere in some lexical scope that continues to hold the intersectionRatio as it changes despite the re-renders, since of course when an element re-renders, the effects that use useEffect technically get their states reset to their default initial values on each render (or, more accurately, they appear to for any code that makes use of the props without adding that prop to its dependency array, outside of the return statement HTML, since the code becomes a stale closure) but almost magically those props can still be used in your HTML code without the resetting ever affecting them, still displaying the values that occurred through past updates and not through initialization, and the same can happen when those props are used in not just the HTML but in other effects, so long as they are added to the dependency array for those effects. It just boggles my mind that we can use functions to maintain state, without making variables to ever hold or change it directly--just allowing the functions to play a game of tag and passing values, mediated by the underlying React library. Of course, even if the state is managed through this game of tag, there is still state, so the code is still not "pure" in the sense of being pure functions, but you can make your component files pure functions, and you can make your effect files pure functions apart from when they use the React library to manage state in just one place--the useState effects at the top of those files.
- Assistant
- User
Indeed! There is another thing that astonishes me, (c) if you will, that you can specify a prop in a dependency array, but the TypeScript compiler says that the prop actually has the same type that it is supposed to have, even a primitive number. This surprises me because primitives are passed by value in JavaScript, so I wonder how React under the hood is able to somehow trigger an update when necessary by such simple props being present in the dependency array. Perhaps React is able to dynamically get the variable name for the prop, and using that information trace it back to when useState was called, to properly track it internally?
- Assistant