5 ms·
Genuine question: why not have a singleton service and store/get data from there(Just like angular services)
by akmittal 9y ago
Genuine question: why not have a singleton service and store/get data from there(Just like angular services)
- cheez 9y agoThe idea is to have a single immutable data store. Immutability gives you, among other things, easier time debugging and reasoning. Having a single, global data store means that the entire state of your application can be managed centrally. There are no seemingly unimportant variables hiding anywhere that would drive your app, it's all in one place.
- xatxat 9y agoYou can do that, but you will need to find a way to re-render the components which use that service, after something changed.
- k__ 9y agoI think this works well for Angular, because it's based on observables. Rendering is just a function from state->ui. If they build their state-container on observables too, the re-render will follow implicitly by subscribing to it.
- ojosilva 9y agoI think that's what mobx is all about. I create a simple Javascript class called WhateverStore and make it observable. Then all the UI components are observer of an instantiated WhateverStore and that's about it.
- remost 9y agoJust do it the React way and re-render everything by calling ReactDOM.render() again. In most cases that is fast enough.
- k__ 9y agoRendering in React is based on state and props. If you want to trigger a render, you need to change one of these. If you base it on some external entity, you need to trigger a render manually. These state libraries usually let you wrap your component in another component, that passes the state down as props, so when the state changes, new props are passed down and your component automatically renders with the new state.