You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Store are a data structure which contains a state and a easy API to update, read and subscribe to it.
Proposed core API:
constnewUserModal=createStore<NewModalUser>({// Initial state is requiredinitialState: {isOpen: false,user:{},role:"USER"},// From allows to parse the data before set it to the statefrom: (data):NewModalUser=>{// Do something with datareturndata}})// Method to update the storenewUserModal.update({isOpen: true,user:{firstName: "John"},role: "ADMIN")// Method to read the storenewUserModal.read()// Method to subscribe the storenewUserModal.subscribe((data)=>{// Do something with the data})// Method to set an errornewUserModal.error(newError("Something bad happened"))// Method to subscribe the storenewUserModal.subscribeError((error)=>{// Do something with the error})// Method to set the default statenewUserModal.setInitialState()
Proposed helpers API:
constopenFormModal=createAction(formModalStore,(prevState)=>({...prevState,isOpen: true}))constcloseFormModal=createAction(formModalStore,(prevState)=>({...prevState,isOpen: false,form: {}}))// With async actions constsetLoadingPosts=createAction(featuredPosts,(prevState)=>({...prevState,isLoading: true}))constfetchPosts=createAction(featuredPosts,async(prevState,page)=>{constposts=awaitapiClient.posts(page)return{isLoading: false, posts }},// It also accept a callback as a third param for sideEffect that should runs at the time of the action execution// but are not directly related to the action itself()=>{setLoadingPosts()})
Proposed hooks API:
useStore works like the useState from React but it is shared
useSubscription Subscribe to the new data events and / or the error event
constformStore=createStore<FormType>({initialState:{}})functionComponent(){useSubscription(formStore,(data)=>{// do something with the data},(error)=>{// do something with error})}
Real use cases and examples:
The text was updated successfully, but these errors were encountered:
What's the difference between a Store and an Event (current API) with a complex state?
Remember that the current event model was derived from the store approach in previous iterations of the state library. And there is a reason for that: Modeling errors like events allow us to keep them isolated from the rest of the logic. This means that, in the store approach, we have to couple data events with error events, the consequence of this is that no other component on the system can subscribe to errors events without subscribing also to data events.
Store API looks like the RXJS API, which we are also are coming from that.
@alcret
be aware that the issue isn't completed there is some things i need to add to be ready, and it is only a proposal i haven't start to coding anything.
Questions:
What's the difference between a Store and an Event (current API) with a complex state?
Remember that the current event model was derived from the store approach in previous iterations of the state library. And there is a reason for that: Modeling errors like events allow us to keep them isolated from the rest of the logic. This means that, in the store approach, we have to couple data events with error events, the consequence of this is that no other component on the system can subscribe to errors events without subscribing also to data events.
Store API looks like the RXJS API, which we are also are coming from that.
The store concept is more complex than event, allows to handle errors, a life cycle which can allows to fetch data or push data to the server. the store API doesn't restrict isolation of errors, nether the problem of having to subscribe to things that the component doesn't. The core API is powerful and simple, the hooks API is that need to optimized for those scenerios.
May looks similar to RXJS, but also the event API this is not a bad thing at all, the main reason is that all use the Observer pattern.
Description
Store are a data structure which contains a
state
and a easy API to update, read and subscribe to it.Proposed core API:
Proposed helpers API:
Proposed hooks API:
useStore
works like theuseState
from React but it is shareduseSubscription
Subscribe to the new data events and / or the error eventReal use cases and examples:
The text was updated successfully, but these errors were encountered: