News for hackers. Stuff that matters.
Re:Solid.js feels like what I always wanted React to be by rk06 on Tuesday March 01, 2022 @12:04AM (#30509862)
The thing is that While react is against side effects, javascript is not. Which result in these impedance mismatch where what devs want is against react itself.

Vue/svelte/solid do not fight against js, hence they do not end up in similar situation

Re:Solid.js feels like what I always wanted React to be by riwsky on Tuesday March 01, 2022 @12:50AM (#30510154)
React lets you build DOM with normal JavaScript loops and .map; solid requires its own For element. How do you define “does not fight against js”? Because the above feels likes solid fighting against js.
Re:Solid.js feels like what I always wanted React to be by peferron on Tuesday March 01, 2022 @04:07AM (#30511551)
Fully agree.

With React, "plain JS" works just like I expect it to. If I have an event handler that does `x = foo` then I don't expect my component to re-render. Why should it? I'm just changing the value of a local variable. If I want to re-render, there's no way to express that in plain JS, so I'll use React's API and write `setX(foo)` instead. Now it's clear that it's not just changing the value of a local variable, but using React's API to do something else.

With Svelte, writing `x = foo` doesn't just change the value of a local variable. Maybe it's going to run a bunch of magic to update a piece of the DOM instead. Or maybe I need to prefix it with `$: ` which in plain JS is a label for a continue or break statement, but here doesn't mean that at all and means reactivity instead.

Oh, and to conditionally render an element, instead of writing idiomatic JS like `condition && <Element />`, I now have to write `{#if condition} <Element /> {/if}`.

WTF. What looks like plain JS is now magic, and what should look like plain JS such as conditional rendering and lists are now some contrived templating constructs.

And almost everyone here is telling me that I should find that simpler somehow.

It makes zero sense.

Re:Solid.js feels like what I always wanted React to be by egeozcan on Tuesday March 01, 2022 @04:24AM (#30511723)
Sorry for the "+1" chain but I want to express my agreement as well.

Especially when I compare it to Svelte and Vue (not solid, solid.js is just... good), if I look at a component, with react it's way easier to say what's going to happen.

Aren't there gotchas? Yes, but sometimes it's just stuff JS lacks, and so does React.

I'm very excited about Records and Tuples in JS to help with the immutability, for example: https://github.com/tc39/proposal-record-tuple

Re:Solid.js feels like what I always wanted React to be by benatkin on Tuesday March 01, 2022 @02:31AM (#30510822)
Don't forget about having to pass key to each element in React. The simplicity of using map() is an illusion.

SolidJS splits it between <For> and <Index>. <For> is equivalent to passing the object as the key and <Index> is equivalent to passing the index as the key.

https://www.solidjs.com/tutorial/flow_for https://www.solidjs.com/tutorial/flow_index

Re:Solid.js feels like what I always wanted React to be by ihateolives on Tuesday March 01, 2022 @03:53AM (#30511422)
> Don't forget about having to pass key to each element in React. The simplicity of using map() is an illusion.

That's one of the design decisions I don't fully understand. It knows that there should be a key there so why just not put it there silently and let me override it when I need, instead of screaming at me when I omit it.

Re:Solid.js feels like what I always wanted React to be by wonnage on Tuesday March 01, 2022 @04:33AM (#30511810)
The purpose is well-documented here: https://reactjs.org/docs/reconciliation.html#recursing-on-ch...

And it also goes into why index is a poor key (it's basically the same behavior as with no key).

Using object identity to detect inserts doesn't work either, because the map function is returning new React element objects on each render.

Practically speaking if you know your items won't change, then omitting the key or using index is fine.

Re:Solid.js feels like what I always wanted React to be by benatkin on Tuesday March 01, 2022 @04:36AM (#30511839)
So SolidJS has this solved elegantly? It lets you choose between object identity and index, and it doesn't have that caveat for object identity because it knows what's going into <For> and not just what's coming out of it.

Don't omit the key in React unless you want warnings.

Re:Solid.js feels like what I always wanted React to be by ihateolives on Tuesday March 01, 2022 @06:58AM (#30513060)
I know, I've read it. I'm still not convinced. React could just as well generate and insert missing key on build instead of asking me to do this:

... you can add a new ID property to your model or hash some parts of the content to generate a key.

It is just plain wrong to ask me to change my data model because of this. This is part of the housekeeping that I expect the framework -- pardon, library -- to take care of for me.

Re:Solid.js feels like what I always wanted React to be by ryansolid on Tuesday March 01, 2022 @11:37AM (#30516324)
It can't. Not in a consistent manner. When diffing user provided immutable data you need a user provided key. Otherwise it can't tell the difference between a new list entry and a nested update. You could treat every nested update as a new item but that is incredibly wasteful as it throws away all descendants. This is something all non-fine-grained rendering libraries have to deal with be it React, Vue, Svelte, or Lit.
Re:Solid.js feels like what I always wanted React to be by mwcampbell on Tuesday March 01, 2022 @03:10PM (#30519331)
How does Solid avoid the need for user-provided keys? I thought it also had to diff the underlying array. Does it check object identity?
Re:Solid.js feels like what I always wanted React to be by ryansolid on Tuesday March 01, 2022 @05:07PM (#30520719)
It does. Just referential check. Our reactivity is nested and we don't want blow out everything so even though there is read/write segregation and immutable interfaces the internals are mutable. In so sorting looks at referential equality, and nested updates don't even trigger list diffing.

Now this does require special process for intaking immutable or big data snapshots where we can't do reference comparison. So we do have a data diffing capability in our nested reactive stores to propagate only what changes. But for the most part common actions like partial updates highly optimized. As well as simple list operations like sorting.

Re:Solid.js feels like what I always wanted React to be by yazaddaruvala on Tuesday March 01, 2022 @04:21AM (#30511699)
It’s a performance pitfall. The warning is trying to make sure the dev correctly tells it how to and when not to re-render.

Could it silence the warning? Yes. However, the React devs choose not to. I think it’s the correct default, but likely needs a more educational warning message.

Re:Solid.js feels like what I always wanted React to be by intrnl on Tuesday March 01, 2022 @02:21AM (#30510742)
i'm finding a hard time articulating what you said, if React is against side effects then useEffect wouldn't have existed and we wouldn't have data fetching.

React is unique in that everything in the component is within the render path, while the rest of the frameworks (that you've mentioned) doesn't.

you might be mistaking "side effects during render is bad" for "side effects is bad", the two statements are not the same.

Re:Solid.js feels like what I always wanted React to be by rk06 on Tuesday March 01, 2022 @03:08AM (#30511088)
I mean side effects from a functional point of view. Let me explain in more details

React’s philosophy is View = F(data)

I.e. view is a pure function of data. By “pure”, we mean F() does not do console.log, ajax calls, date time and other stuff which is not consistent every where.

This assumption is ingrained in React. You see it when you are told that react can overrender and your code should handle overrendering. And react works well when user code is pure. However, real world does not work that way. So, there is a need to handle side effects, so that “side effects” works well with pure function assumption of react. This “handling of side effects” is the over head introduced by react.

Other js frameworks (svelte, solid, vue) do not assume full immutability or pure functions, hence User code does not need special handling for these cases

Re:Solid.js feels like what I always wanted React to be by benatkin on Tuesday March 01, 2022 @03:14AM (#30511125)
> View = F(data)

That's just how templates are meant to work. Underscore.js templates don't allow making an AJAX call before calling render() either. https://underscorejs.org/#template

Re:Solid.js feels like what I always wanted React to be by rk06 on Tuesday March 01, 2022 @03:37AM (#30511281)
Consider following scenario:

User is entering an account register form. When the user has entered a value in nickname, your app must check if it is in use and display error message if the nickname is already in use.

Sounds good, and ubiquitous right? Well, that also require an ajax call to server for validation, so it breaks the pure function assumption right there. Now, you what do you do?

Re:Solid.js feels like what I always wanted React to be by benatkin on Tuesday March 01, 2022 @04:08AM (#30511566)
The template's render function doesn't make the AJAX call - not in React, and not in Underscore templates.

When you display the error message, yes, React provides a way to do that without re-rendering the input box and while keeping the contents, but many react devs are skipping the traditional React way of doing that and using react-hook-form instead.

I get what you're saying about the component's data updating the state of the DOM in a way that resembles pure functions, but I think AngularJS did it before React, and that Backbone was not far from this vision. Certainly there are a zillion JavaScript frameworks that do this now, and only React has you jumping through silly hoops like "className" and "htmlFor". https://preactjs.com/guide/v10/differences-to-react/#raw-htm... https://www.solidjs.com/tutorial/bindings_classlist

Re:Solid.js feels like what I always wanted React to be by kevinmcconnell on Tuesday March 01, 2022 @04:17AM (#30511657)
> so it breaks the pure function assumption right there

There's no pure function assumption being broken here. React is a framework for rendering UI from state and coordinating updates to that state. That's why we have things like `useEffect`, contexts, and so on. The only part of React that is expected to be free of side effects is rendering.

Put another way, given your example, React just says that you shouldn't issue your Ajax call in your rendering code. Instead, you should do it in response to an appropriate action, such as a change event on your form controls.

Re:Solid.js feels like what I always wanted React to be by toomanydoubts on Tuesday March 01, 2022 @04:31AM (#30511787)
I fail to see the purity in react with all that useState, which obviously by the name is tracking state outside the scope of the function(eg: it's actually View = F() and data is coming from outside when calling useState). It's not pure at all. I really would like to see something more akin to Elm, where all your state is explicitly passed into the component, and you have commands that update state and trigger the view again, making it actually "functional". I guess this takes the form of redux in react, but I'd like to see it ingrained in the components themselves.
Re:Solid.js feels like what I always wanted React to be by imtringued on Tuesday March 01, 2022 @09:26AM (#30514502)
Yes it's var View = F() and var hidden_global_variables_for_hooks;
Re:Solid.js feels like what I always wanted React to be by LAC-Tech on Tuesday March 01, 2022 @05:21AM (#30512268)
> React’s philosophy is View = F(data)

Yeah internal component state throws all of that out the window.

That's lit-htmls philosophy, not react. React is all about bundling the view and state and then marathon profiling sessions to figure out why everything is re-rendering all the time.