by jchw · 3y ago · 0 comments
The main reason why React is still popular is, drum roll please... The programming model. JSX is not an antiquated idea, it is still one of the better ways to integrate the UI part of a JS application into the JS part directly. I greatly prefer JS logic inside of my HTML versus a bespoke template language specifically because it's easy to compose and construct complex logic in.
I've been messing around with Svelte a bit in spare time. I really like Svelte and will probably continue to use it, but the two things I will note is:
- The integration with Web Components is imperfect and doesn't really hit me as something I would seek out.
- The templating logic in the HTML feels decidedly inferior versus just being able to use JS logic and JSX. Try doing a for loop where you count up; the best answer I could find on the internet was to construct an Array using `Array(number)` and enumerate over it...
What I really want is actually more like React's programming model with Svelte's "compiling down to nothing" mantra.
But this Web Components fervor, I know people get very heated about it, but I strongly believe that in 10 years it's going to be one of those grotesquely complex legacy features nobody can get rid of.
https://github.com/AlexErrant/Pentive/blob/main/example-plug...
It is very small and very quick - one of the fastest frameworks out there - but also not too complicated or lean. The signals system takes a bit of getting used to, particularly if you're coming from the React world (e.g. there are no render functions, rather each attribute is reactively updated when its data dependencies update), but it's also incredibly powerful once it clicks. Also, once you get down to it, almost everything is just native browser elements, so if you need to integrate with another tool or library, it's usually really obvious how that should work.
This was also the case (-ish) for Vue, Svelte, Solid. Their authors were really bullish on web components in the beginning. Now they are all on the scale from extremely negative towards them to completely indifferent.
render(props) {
return(
`<div>
{props.value}
</div>`
);
}Angular doesn't separate concerns at all, it just ties it all together with an entirely new and un-intuitive DSL that you have to now write in the HTML.
Just look at your DSL here: https://angular.io/guide/binding-syntax#types-of-data-bindin...
You abso-fucking-lutely are writing code in your html, you're just writing a crippled version of their custom DSL instead of plain old javascript.
Frankly - I much prefer to write plain old javascript instead of learning a new bastard language that isn't useful anywhere else.
JSX achieves that relatively well. It's not entirely plain old javascript. But it's about as close as I've seen anyone get for a template language complete with data binding.
EJS https://ejs.co/ was my pick before JSX. For similar reasons - I don't want to have to learn a dsl which is just a shoddy wrapper for the JS anyways. Just let me write the damn javascript.
Why is this important to maintain? People have been complaining about this since the beginning, but I've never seen anyone actually articulate what the problem is.
So PHP?
Mixing html and code is a pretty old paradigm.
I'm taking about HTML/JavaScript front-ends. The ideas behind JSX go back to at least 2004 when I first began using that paradigm.
But please, build something proper with just that and presumably a .innerHTML = ...
Let us know how it goes :)
But with a "tagged template" it should be easy to treat the variables before actually doing the templating, without making the code ugly.
Not to mention holding focus and other element state, would that just get blown away and replaced with a whole new element every time? (Where react does tree reconciliation to avoid unnecessarily blowing away and replacing elements)
Maybe you’re only addressing the point about programming model, but this approach seems like it has some issues.
{#each {length: 3} as _, i}
https://stackoverflow.com/questions/58213585/svelte-3-how-to...Or do you create an array of JSX-elements, like
let elements=[]
for (let i=0; i < 10; i++) {
elements.push(<li key={i}>I'm number {i}</li>)
}
return <ol>{elements}</ol>;
Does not seem like an improvement to me.Just use the normal event handlers. If you're tied into a store (ex: redux/zustand/recoil/context/etc) just update the store from the non-react library and things will work themselves out just fine.
You can watch the DOM with mutationObservers, but if you own both pieces (react and non-react) why would you bother? They can both coordinate just fine.
MutationObservers really shine when you happen to missing an eventHandler you want in a library, or you're running on someone else's DOM so you don't control both sides (ex: you're an extension content_script).
I'm not convinced that people are building integration APIs for consumption by external code. That means no JS events (redundant since it uses stores or signals and whatnot) and minimal callback availability to hook into a component state.
One might not control the code if this is from an open-source library for instance.
Maybe the premise is "don't do that" , but then people still wanted WebComponents at some point. So seems there exist use-cases for component interaction. Or were people mistaken in the first place?
I own both sides of the stack. Say I have a react component that happens to mount a svelte component in the DOM.
I expect Svelte code to be running somewhere (I wanted to use a svelte component, I have to initialize that stack).
I expect React code to be running somewhere (The main page is react, for example).
I expect to have some sort of signaling or state management solution (say a redux store, since the main page is react).
I can just update the redux store from both stacks. The store emits the correct events whenever it's updated - all subscribers are notified - react-redux does its magic and conditionally re-renders my react tree based on the new state.
I can do exactly the same thing in Svelte - subscribe to store events, see an events when react changes something, update my svelte app in response.
---
And it's absolutely not limited to redux... I can do the same thing by hand with global state, or by emitting custom events in the DOM, or any of a hundred other ways to get these two apps to work together. I own both sides - making them play nice is normally trivial (if not always quick, and sometimes tedious as all get out).
this doesn't happen in practice... like at all...
is your implicit assumption that anything written in react will automatically work just fine with svelte but not the other way around? why would react -> svelte be any easier than svelte -> react, and why is the burden on React to solve interoperability problems between competing frontend frameworks?
Solid JS!