Replies: 5 comments 16 replies
-
Is this something that we could do in Userland? Maybe with an API like this: export default Cell({
query: listPostsQuery,
loading: Spinner,
failure: ErrorDisplay,
PostList({ data: posts }) { ... }
})
|
Beta Was this translation helpful? Give feedback.
-
Spinning out @AndriusBartulis point to declutter:
|
Beta Was this translation helpful? Give feedback.
-
Pinging @peterp — you said you had thoughts on this? |
Beta Was this translation helpful? Give feedback.
-
@samwightt's implementation is exactly how we think of it in RedwoodJS; with the exception of a few things: We have guard functions that sandwich We also include a mechanism for "refetching:" If your client library invalidates the cache and refetches the data we pass an additional prop into success named On a personal note I enjoy the way that Cells let me think about my components in insolation: The shape of the "loader", the failure state, what they're fetching, etc. |
Beta Was this translation helpful? Give feedback.
-
I'm worried that making data fetching easy to compartmentalize into Cell components is going to lead to network waterfall issues. Maybe you have a Cell that is a parent of a Cell that is a parent of another Cell. Each child can't fetch data until the previous Cell's data has arrived, creating the network waterfall effect. It exaggerates the impact of the most time-consuming aspect of fetching data, the time it takes to send stuff across the wire. This seems like one of those design issues that you can sweep under the rug at first because the queries are "fast enough," until the day they aren't. And on that day you have to undo all of that component isolation that a pattern like this creates. Scoping Cells as BlitzPages resolves the waterfall issue, but at that cost of having something that seems generally worse than server-side rendering (except by having a better error handling story out of the box), but page-like Cells don't have a way to receive query parameters unless they're in the query string. |
Beta Was this translation helpful? Give feedback.
-
Background
Currently the
useQuery
hook is used to load data from the server into components.useQuery
works great, but rendering different UI on the basis of a query's state can be a bit of a pain / involve a lot of boilerplate.For an example of this, let's use a component that renders out a list of posts from the server. We want to display different components for the loading, failure, empty, and success cases. We want these states to only be applied to this current component: we're okay with having multiple spinners or error messages in the app. This might look something like this:
We wrap the main component with a
Suspense
andErrorBoundary
, and then export that new component to get the loading states displayed.This is a pattern I've found myself repeating a lot in my Blitz code. It results in a lot of boilerplate when I just want to write a hacky component that manages its own loading and error states.
Cells
To help mitigate this boilerplate, I'd like to propose a Cells API. This would be implemented in mostly the same way as the RedwoodJS Cells API. A cell is a module that can have a few different exports:
query
: The query to call.Loading
A component that should render when the query is loading.Empty
: A component that should render when the query isnull
or an empty array.Failure
: A component that should render if there's an error.Success
: A component that should render if the data has loaded.Webpack would combine these exports into a single component, writing the boilerplate code above automatically for you. Any file ending in
.cell.tsx
would be assumed to be a cell by Webpack and would be compiled as such.Example
Here's an example of what the above would look like rewritten as a cell:
The different states are separated and the boilerplate code is gone. It's easier to read, and the loading state stays closer to the component's definition, instead of having to hoist it further up the tree.
What are y'all's thoughts on something like this?
Beta Was this translation helpful? Give feedback.
All reactions