-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
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
Entity loader/reader React components #19
Comments
Unified single loader component with optional render props. <ResourceLoader
resource={'member/all'}
resourceId="123" // <- not defined = resource LIST
// prop passed = resource DETAIL
entityType="member" // <- not defined = not normalized (not entity) value set results normalized
// prop passed = response normalized into entities
// all render render props optional and default to return `null`
renderInitial={() => <p>Not Loaded</p>}
renderLoading={() => <p>Loading...</p>}
renderError={(/*error*/) => <p>Error!</p>}
renderSuccess={memberList => memberList.map((member) => (
<li key={member.id}>{member.name}</li>
))}
loadOnMount // <- when if not set data doesn't load until one of the load
// functions `loadResource`/`loadResourceData` are called.
//
>
{({data: memberList, loadResource, loadResourceData, status, statusView}) => (
<div>
{/* `loadData` is a function
* object provides boolean flags useful for conditionally
* rendering content.
*/}
<div>
<button onClick={requestResource}>(Re)load Data</button>
</div>
{/* `status` object provides boolean flags useful for conditionally
* rendering content.
*/}
{status.initial && <p>No Request Made Yet</p>}
{status.error && <p>Error!</p>}
{status.pending && <p>Loading...</p>}
{status.success && memberList.map((member) => (
<ul>
<li key={member.id}>{member.name}</li>
</ul>
))}
{/* `statusView` is populated by the render prop matching the current
* `status`.
*
* renderInitial ~> status.initial === true
* renderLoading ~> status.loading === true
* renderError ~> status.error === true
* renderSuccess ~> status.success === true
*/}
<ul>
{statusView}
</ul>
</div>
)}
</ResourceLoader> |
Closed by #21 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Overview
Loading and reading entity data doesn't need to be handled by specific containers for every component that needs data. Render Prop components for loading and accessing entity data are a power solution which allows you to bypass writing all that boilerplate.
Current Usage Example
Loading and accessing an entity currently requires you create a container component that you wrap the component(s) that need to access the entities.
Click to Show Example(s)
Container
Component
Desired Usage & Components
The need for a container component is removed all together when using the following two components to load data.
ResourceListLoader
This component loads the resource and returns the results. If the
entityType
prop is provided the resource is normalized into entities and the entities are
returned.
ResourceDetailLoader
This component loads a resource detail (single item) and returns the results. If
the
entityType
prop is provided the resource is normalized into an entity andthe entity is returned.
EntityDetail
This component pulls an entity detail (single item) from the entities state.
Note that this component does NOT load data like the loaders above.
EntityList
This component pulls an entity list (multiple items) from the entities state.
Note that this component does NOT load data like the loaders above.
Implementation Checklist
Components
Load & Return Data (resource request)
Read Data (data pulled from entities state)
The text was updated successfully, but these errors were encountered: