Separating view and logic
It's generally good practice to separate view components from logic components. Async components should preferably be logic-only. That means they don't render anything by themselves. Instead you can use the render props pattern to pass down the async state:
ErrorMessage
andGreeting
would be separate view components defined elsewhere.
Cleaning up the JSX
You'll notice the render props pattern is very powerful, but can also lead to code that's hard to read and understand. To make your JSX more declarative and less cluttered, you can use the <Async>
component and its state helpers. These take away the need for if/else
statements and return
keywords in your JSX.
You should know that these helper components do not have to be direct children of the <Async>
component. Because they are automatically wired up using Context, they can be placed anywhere down the component tree, so long as they are descendants. You can also use helpers of the same type, multiple times.
Stand-alone versions of <Async.Pending>
and the like are also available. However, these must be wired up manually by passing the state
prop and are therefore only really useful when combined with one of the async hooks.
Last updated