DEV Community

Discussion on: Converting a React Component to SolidJS

Collapse
 
alexanderpomazan profile image
Alexander-Pomazan

Small suggestion about calculating the bufferGap value:
I understand that it is probably here for the sake of example of createEffect usage, but you could avoid having unnecessary rerender caused when updating bufferGap state by storing the state dependant value in a ref. It could look something like this:

const useBufferGapValue = (state) => {
   const bufferGapRef = useRef(null)

   if (isConditionMet(state)) {
      bufferGapRef.current = calculateNewValue(state)
   }

   return bufferGapRef.current
}
Enter fullscreen mode Exit fullscreen mode

Because we store the value in a ref, it allows us to perform all calculations without cascading state updates. We would avoid unnecessary calculations since we only update the ref value the condition is met.
I believe something like this should be possible in Solid

Collapse
 
mbarzeev profile image
Matti Bar-Zeev

This is where SolidJS shines over React IMO (at least one of the main places).
The component function is called only once. There will be no unnecessary renders.
No need for ref. You know exactly the state of your component as it only reacts to changes and not renders in its entirety.
This is freaking awesome, and sooooo simple to reason about.