r/reactjs • • Nov 18 '23

Discussion How I wanted to improve React

https://medium.com/weekly-webtips/how-i-wanted-to-improve-react-4108d5052aaf

[removed] — view removed post

1 Upvotes

7 comments sorted by

2

u/fii0 Nov 18 '23

My goodness gracious, have you heard of Svelte? It addresses all of the same problems, without the drawback of needing to explicitly call a component.update() function. You can claim that it's "less flexible" and avoids "mixing of responsibilities". Not even disagreeing with the idea that it's mixing responsibilities, but if I am doing rendering logic outside of a <canvas> and using window.requestAnimationFrame(), I do not want to be responsible for handling rendering so explicitly, I find it quite convenient to set a variable and see the new result rendered automatically (behind the scenes with setter functions in React, or by using the value directly with useRef or Svelte's approach).

So that's just my first impression, based on looking at the code:

const component = button(
  {
    click$e: () => {
      state += 1;
      component.update();
    }
  },

  "Clicked ",
  () => state,
  " times."
);

the method of attaching event listeners looks like a worse Svelte plus I don't like the explicit rendering call for reasons mentioned, but the rest of it bothers me as well - the idea of needing to use seemingly endless function args for children of a component absolutely terrifies me.

Also, the idea of not being able to use basic HTML markup and instead needing to call functions like div(p("Hello World!")) also absolutely terrifies me. You're incorporating some of the things that I dislike the most about Vue and Svelte - introducing a new API for event listeners that doesn't closely match the HTML spec like React does - while also introducing an arbitrary and less clear way to render HTML elements that none of those other popular libraries use, though I see that you're supporting JSX as well, just with the need to still call functions to access stateful values. Bleh.

Obligatory "the totally real React Forget engine will solve most of these problems"™

1

u/isumix_ Nov 18 '23

There is nothing difficult in calling an `update` once in a while only where it is needed. There are several example apps implemented this way.

Moreover, Fusor library does not have state management built in. The examples I've provided use plain variables. This means you can employ your state management where you do not have to update yourself.

`setState` in React is an `update` function which you also have to call manually.

Event handles attachment closely follows w3c spec and 100% compatible, see: https://github.com/fusorjs/dom/blob/main/REFERENCE.md#dom-attributes-properties-events

JSX support is there if you don't like functional notation.

Svelte hides away its innerworking which can lead to suboptimal resuls. Also it introduces its own syntax for reactivity, control structures, file structure... This adds more cognitive load.

1

u/fii0 Nov 18 '23

There is nothing difficult in calling an update once in a while only where it is needed. There are several example apps implemented this way.

My gripe isn't that it's difficult, it's that it's unnecessary. I think the abstractions for rendering provided by React and Svelte are helpful ones.

The examples I've provided use plain variables

"Plain", yet you need to use anonymous functions everywhere to access them, for some reason...

Event handles attachment closely follows w3c spec and 100% compatible

Ohh I see, you specify attribute or property, or let it be automatically determined. That's cool, I like it.

1

u/isumix_ Nov 18 '23

My gripe isn't that it's difficult, it's that it's unnecessary. I think the abstractions for rendering provided by React and Svelte are helpful ones.

I aimed to dispel this common misconception in the article. The verbosity and opacity of React and other frameworks not only make code more challenging to handle but also impact performance.

"Plain", yet you need to use anonymous functions everywhere to access them, for some reason...

I didn't quite understand. Could you elaborate, please?

1

u/fii0 Nov 18 '23

The idea that "Hooks make the code more verbose and harder to understand" is not substantiated by the article because the idea that you need to always "write wrappers for simple operations" is not true. Sometimes, sure, but pretty rarely.

Performance with hundreds of components is rarely a concern in React, it has an earned reputation for being very fast - don't know about thousands though - but in the rare cases where re-renders become a concern, React lets you just bypass the VDOM.

I didn't quite understand. Could you elaborate, please?

"Clicked ",
() => state,
" times."

Here, you have to create a getter function to get the value of a prop which is just a number. Why?

1

u/isumix_ Nov 18 '23

Exactly, when the app gets bigger, utilising thousand compopnents, then things are getting slower.

() => state this is how dynamic values a passed to components, and when you call update are populated to DOM.

If you would pass just state there, the constructor function button would return just static html button element, not Fusor component.

1

u/isumix_ Nov 18 '23

Check out this example please, it has more moving parts https://codesandbox.io/s/fusor-analog-clock-jsx-hqs5x9?file=/src/index.tsx