6 ms·
By making it explicit and a bit clunky, you discourage users from using state. Without explicit functions to be called, you can't detect state changes and woul
by Swizec 12y ago
By making it explicit and a bit clunky, you discourage users from using state.
Without explicit functions to be called, you can't detect state changes and would need polling. This is terrible.
At the end of the day, it's a limitation of JavaScript because unlike with Python, for example, you can't have automagic getter/setter functions. They have to be called as functions.
- nulltype 12y agoSeems like .observe() would fix that polling thing, right? http://www.html5rocks.com/en/tutorials/es7/observe/ http://www.html5rocks.com/en/tutorials/es7/observe/ Also you could just re-render whenever any event happens, which is probably when your state changes anyway.
- Swizec 12y agoIt would if browsers supported it: http://caniuse.com/#search=observe http://caniuse.com/#search=observe So far exists only on Chrome and Android browsers.
- lopatin 12y agoOne reason for the explicit method may be that React does an internal optimization where it batches state updates together. Two calls to setState can result in just one render. https://groups.google.com/forum/#!msg/reactjs/R61kPjs-yXE/ysmpC0IvA4YJ https://groups.google.com/forum/#!msg/reactjs/R61kPjs-yXE/ys...
- masklinn 12y agoNo reason why this wouldn't be possible with Observe, the first call schedules a re-render and the next one doesn't do anything since there's already a re-render scheduled.
- cgag 12y agoObserve just makes making bad apis easier imo. I don't care if react can detect state changes, I want to detect state changes by searching for setState.
- masklinn 12y agoA possible issue is setState doesn't transition the object's state, it creates an async future/pending state transition (and multiple state transitions can be batched). Object.observe can only operate reactively, the state has already changed and the next dereferencing of state data will see the "next state" instead of the "current state".
- straws 12y agoActually, custom getters and setters exist in ES5: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#Defining_getters_and_setters https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guid... AmpersandJS, for example, implements its observable pattern in this way: http://ampersandjs.com/learn/state http://ampersandjs.com/learn/state That said, I don't think the pubsub/rx/observables pattern is the most optimal for UI development, since it still allows for cascading event chains. Modeling all application state as a large data structure that allows for efficient diffing — which nicely mirrors the diffing react does with both it's internal component tree and diffing with the DOM — is a much more straightforward approach. Just re-render your application 60 times a second, like a game engine, and make that process as efficient as possible.
- nulltype 12y agoThis is exactly where I am hoping reactive ui ends up. If you don't want to do 60 times a second you can render only on certain events like network call or mouseover. If you do the rendering in requestAnimationFrame you can batch things easily.