We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
We are taking a review of the patterns implemented to solve common situations.
In this scenario, we need to fetch data only once. An example of this can be form data, data that is only fetched once for the view to render.
events
const SuccessEvent = createEvent(); const ErrorEvent = createEvent();
action
const fetchUsers = (someId: int, dispatch = true): Promise<UsersList> => { // permissions // validations let response; try{ response = fetchResource(); }catch(e){ dispatch && ErrorEvent.dispatch(e); throw e; } dispatch && SuccessEvent.dispatch(response); return response; }
Component
const Component = (): React<Component> => { const [users,setUsers] = useState(SuccessEvent.get()); const [loading,setLoading] = useState(true); useEffect(()=> { fetchUsers(); }, []); useSubscription(SuccessEvent, (users)=>{ setLoading(false); setUsers(users); }); useSubscription(ErrorEvent, (e)=>{ setLoading(false); onError(e); }); render (); }
dispatch
The use of a hook that handles the 3 states of the fetch.
const fetchUsers = (someId: int, dispatch = true): Promise<UsersList> => { // permissions // validations let response; try{ response = fetchResource(); }catch(e){ dispatch && ErrorEvent.dispatch(e); // OPTIONAL throw e; } dispatch && SuccessEvent.dispatch(response); // OPTIONAL return response; }
const Component = (): React<Component> => { const [users,loading,error] = usePromise(()=> fetchUsers(SuccessEvent.get())); if (error) onError(error); render (); }
@alacret this seems like a pretty reduction of the code required to use the actions/events
TO-DO: @jesusrodrz
In this scenario, we need to fetch data depending on changes in the component. An example of this can be a ListView with filters and conditions.
const Component = (): React<Component> => { const [users,setUsers] = useState(SuccessEvent.get()); const [filter,setFilter] = useState(0); const [loading,setLoading] = useState(true); useEffect(()=> { fetchUsers(filter); }, []); useSubscription(SuccessEvent, (users)=>{ setLoading(false); setUsers(users); }); useSubscription(ErrorEvent, (e)=>{ setLoading(false); onError(e); }); render ( <> <SomeComponente onClick={()=> {fetchUsers(filter)}} </> ); }
TO-DO: @jesusrodrz ref: #19
@alacret it seems that the createAction is really a decorator. Doesn't seems to change much the behavior or improve the use of the dispatch parameter.
createAction
By extending the current functionality of useSubscription to support multiple events.
useSubscription
Ref: #17
const Component = (): React<Component> => { const [users,setUsers] = useState(SuccessEvent.get()); const [filter,setFilter] = useState(0); const [loading,setLoading] = useState(true); useEffect(()=> { fetchUsers(filter); }, []); useSubscriptions([SuccessEvent, ErroEvent], (users, e)=>{ setLoading(false); setUsers(users) if (e) onError(e); }); render ( <> <SomeComponente onClick={()=> {fetchUsers(filter)}} </> ); }
TO-DO: @alacret
The text was updated successfully, but these errors were encountered:
REQUIREMENT:
import React from "react"; import "./styles.css"; // Alternativa 1 const testAction = async (id, dispatch = true) => { // validations let result; try{ result = await client.query(); }catch(e){ dispatch && Error.dispatch(e) } try{ result = await action1(dispatch); }catch(e){ throw e; } try{ result = await action2(dispatch); }catch(e){ throw e; } dispatch && Event.dispatch(e) return result; }; // Alternativa 2 const testService = async (id) => { // validations let result; try{ result = await client.query(); }catch(e){ throw e; } try{ result = await service1(); }catch(e){ throw e; } try{ result = await service2(); }catch(e){ throw e; } return result; } const testAction = async (id) => { let result; try{ result = await testService(); }catch(e){ Error.dispatch(e) } Event.dispatch(e) Event1.dispatch(e) Event2.dispatch(e) return result; }; // Alternativa 3 const testService = async (id) => { // validations let result; try{ result = await client.query(); }catch(e){ throw e; } try{ result = await service1(); }catch(e){ throw e; } try{ result = await service2(); }catch(e){ throw e; } return result; } const testAction = createAction(testService, [Event, Event1, Event2], [Error]);
Sorry, something went wrong.
No branches or pull requests
We are taking a review of the patterns implemented to solve common situations.
Purpose
General Constraints:
USE CASES:
1. Simple fetching
Description:
In this scenario, we need to fetch data only once. An example of this can be form data, data that is only fetched once for the view to render.
Solutions
Current Solution:
Known problems:
dispatch
parameter is required when multiple mounted components depend on the event.Alternative Solution 1 (@jesusrodrz )
The use of a hook that handles the 3 states of the fetch.
Comments:
Alternative Solution 2 (@jesusrodrz )
TO-DO: @jesusrodrz
Comments:
2. Fetching with dependencies
Description:
In this scenario, we need to fetch data depending on changes in the component. An example of this can be a ListView with filters and conditions.
Solutions
Current Solution:
Known problems:
dispatch
parameter is required when multiple mounted components depend on the event.Alternative Solution 1 (@jesusrodrz )
TO-DO: @jesusrodrz
ref: #19
Comments:
Alternative Solution 2 (@alacret )
By extending the current functionality of
useSubscription
to support multiple events.Ref: #17
Comments:
3. Delete / Update Actions
The text was updated successfully, but these errors were encountered: