by satvikpendem · 3y ago · 0 comments
This is a good thread about their drawbacks [2]. Apparently, both the below examples actually do different things:
function One(props) {
const doubleCount = props.count * 2;
return <div>Count: {doubleCount}</div>
}
function Two(props) {
return <div>Count: {props.count * 2}</div>
}
Like the tweeter says, signals are mutable state. I'll stick with unidirectional data flow in React.[0] https://twitter.com/jordwalke/status/1629663133039214593
[1] https://twitter.com/dan_abramov/status/1629539600489119744
[2] https://twitter.com/devongovett/status/1629540226589663233
1. The function is called only once at component construction, and never again. Unlike React, it is _not_ called for every render.
2. Every expression in `{}` is compiled to a thunk function - not to a direct JS expression.
For example, the expression
<>{props.count * 2}</>
compiles to memo(() => props.count * 2)
Another interesting bit - as the article above rightfully points out, `useState` and `useMemo` are signals. The main difference is that you have to track and specify your dependencies manually, whereas Solid auto-tracks dependencies based on their usage while the computation runs.Ideally, signalling shouldn't be retrofitted on normal variables. It assumes then that you overload variable access (assignment and reads). Then it's hard to know which variables are reactive and which ones are not. The problem stems from the overloading of semantics.
A UI is mutable so it's not about mutable state being wrong but the unit of variability are not program variables but rendered props which are basically DOM Element properties. In react, it just happens that it uses variables whose values are injected in the DOM elements via jsx but that's an issue (especially when dealing with exports, prop drilling etc)
So until a plain variable and a reactive variable are made to look different everywhere in code, it will remain confusing. And even then, it's not optimal.
Disclaimer: I'm currently implementing a cross-platform UI framework in Go and that's the reason why I had to think about it since traditional languages do not have reactive variables.
I’ve implemented this pattern with mobx and those two pieces of code are equivalent.
In my opinion:
- Unidirectional data flow patterns prevent a class of problems junior developers tend to make but require much deeper discipline to keep the app understandable
- Conventional bi-directional data flow patterns are susceptible to junior developer mistakes but generally are hard to make unreadable
The state has to go somewhere, curious how you choose to handle it.
If you don't like what Solid is doing you may consider it a self-inflicted wound, nothing is forcing Solid to work that way.
I love signals in VueJS, and it doesn’t even preclude a redux-like/lite state- management library like Pinia.
https://vuejs.org/guide/extras/reactivity-in-depth.html#conn...
"patch languages" like PureData, Max/MSP/Jitter, QuartzComposer are much better interfaces to work with signals ... Or rather with signal processors.
- The Evolution of Signals in JavaScript, https://dev.to/this-is-learning/the-evolution-of-signals-in-...
- React vs Signals: 10 Years Later, https://dev.to/this-is-learning/react-vs-signals-10-years-la...
- Making the Case for Signals in JavaScript , https://dev.to/this-is-learning/making-the-case-for-signals-...
Those two code examples are different because that's the tradeoff that Solid made. It allows Solid to track changes in a uniform way even if the data isn't inside a component. Because in Solid components are just a way to organize code, not a unit of rendering (like in React).
And on top of that if you think that React still somehow has unidirectional data flow, it doesn't. Hooks make it anything but unidirectional. And not that different from signals: https://res.cloudinary.com/practicaldev/image/fetch/s--upMC6... But often less predictable.
See also The Cost of Consistency in UI Frameworks, https://dev.to/this-is-learning/the-cost-of-consistency-in-u... which is slightly related to this.
And you never use only local state. You advocate for Redux in a different comment which is literally a global state where you are going to have something suddenly change somewhere from the point of view of a component.
For Redux, at least that's explicit, for signals, it's really not.
It is hard to even make sense of the discussion without imagining the details of how those frameworks are implemented. And the discussion on the level of normal usage, not something advanced or development oriented.
So, I'd guess, something with referential transparency.