Showing posts with label frontend development. Show all posts
Showing posts with label frontend development. Show all posts

Sunday, March 24, 2024

Advanced React Hacks 5/10 - "Old School" Events

Another "advanced" tip coming!

Consider the following situation
  • You're required to communicate between components in different locations at the component hierarchy. Perhaps they aren't even in a parent child relationship
  • Some functions are involved, maybe some loops. This code is already done, and already works.
  • Perhaps, some refs or other outside-of-React escape hatches are involved. This code is already done, and already works
  • And of course the usual restriction of a "professional" software developer -- don't change too much, and anything you change must be regression tested and so on and so on!
You consider several solutions
  • Since functions and loops are involved, you can't use hooks (at least not inside the functions or loops)
  • The code is already done; you don't want to refactor everything and besides you're not sure that refactoring everything will lead to a better solution anyway because it's a one-off
    • Ideally all the information should have been stored in a central location and flow down to all the required components, but that bus is gone
      • Besides, the days of storing a property, modifying it with two-way data binding and binding to that property are long since over (this isn't Knockout or Backbone!)
        • Besides, even if you did it, you would have to modify enormous amounts of already completed code, possibly create regressions, and pass down props many levels!
  • Seems like a perfect situation to use Redux (Redux Toolkit) or some other state management but besides the fact you can't use hooks, you don't want to mess with the global store and it seems wrong to use it for the one-off
  • You look at some solutions like HTML5 local storage, but that's already outside of React and you would somehow have to subscribe to local storage anyway
    • Besides, the usehook-ts package doesn't properly install
      • Besides, after copying the usehooks-ts package, it's a hook, so you can't use it in the functions anyway!
What do you do now? Is React so obtuse that you really can't deal with this situation in any way?


Are you doomed to a sprint carryover and total humiliation, just because React sucks monkey balls?



There is an answer
Forget React
Forget Hooks
Forget Frameworks
Forget react.dev documentation (except for this )


JAVASCRIPT




// inside a custom hook if you want to reuse this...
// put custom hook inside component you want to rerender...
// or just put this inside the component...

useEffect(() => {
  const func = {
    // code to subscribe to...
  }
  window.addEventListener('cool-event-name-make-it-constant', func);
  return () => window.removeEventListener('cool-event-name-make-it-constant', func);
});

...

// inside the function you want to trigger an event from
// Normal JavaScript!
// setTimeout is the secret sauce when dealing with refs! (may not be required)
setTimeout(() => {
  window.dispatchEvent(new Event("cool-event-name-make-it-constant"));
}, 10);



So remember the basics, and when React fails you, just use ordinary JavaScript

The dispatchEvent() method of the EventTarget sends an Event to the object, (synchronously) invoking the affected event listeners in the appropriate order. The normal event processing rules (including the capturing and optional bubbling phase) also apply to events dispatched manually with dispatchEvent().

Calling dispatchEvent() is the last step to firing an event. The event should have already been created and initialized using an Event() constructor.

The best documentation

Sometimes the old ways are the best ways


Hope this helps someone!

Friday, February 9, 2024

Advanced React Hacks 4/10 - Prevent Rerenders

Advanced React Hacks 4/10 - Prevent Rerenders


Very obvious when you see the answer (and actually documented on react.dev, link later) but still worth mentioning for posterity.

You don't want to render too much, because one point of React is that it minimises the amount of rerenders on a screen (or is supposed to)


How do most working React developers see this? With the "highlight updates" tool of course


But, it is worth mentioning (in case it happens again, and for a lesson) that in 2019 with React Developer Tools v4, this killer feature was removed


To the React Team's credit, they restored "Highlight Updates" quickly

Anyway how do you prevent re-rendering? It's actually easy

Once the component has been initially rendered, you can trigger further renders by updating its state with the set function. Updating your component’s state automatically queues a render. (You can imagine these as a restaurant guest ordering tea, dessert, and all sorts of things after putting in their first order, depending on the state of their thirst or hunger.) React.Dev "Managing State"

If you see too much of your screen rendering making it slow when taking actions (like clicking) break it down



Make more components... obvious, but worth mentioning the obvious

Until next time...