Storybook - Using loaders

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Loaders are still an experimental function that can be used to add data for a story, and it's decorators.
The loaders are run before the story renders, and whatever is loaded gets injected into the render context. You can use these loaders to load any asset or even fetch data from an API.
We get two ways of loading that we'll look into, one is for a specific story, and one is for your global system.
Let's say we have a specific item that needs rendering instead of mocking the hard-coded data. We can opt to load it from a remote API.
This could be a mock API, as you'll see in the example, or your actual API if it's public data.
Let's take the example of a story that renders a blog item card.
import React from 'react';
import fetch from 'node-fetch';
import { BlogItem } from './BlogItem';
export default {
title: 'BlogItem'
component: BlogItem,
};
export const Primary = (args, { loaded: { blogItem } }) => <BlogItem {...args} {...blogItem} />;
Primary.loaders = [
async () => ({
blogItem: await (await fetch('https://jsonplaceholder.typicode.com/posts/1')).json(),
}),
];
Now our item will load whatever the API returns. This could be useful to try against a vast majority of randomized test items.
In some cases, you might want to prepare a global loader, for instance, a logged-in user attribute.
We can add these loaders into our .storybook/preview.js file like this.
import fetch from 'node-fetch';
export const loaders = [
async () => ({
currentUser: await (
await fetch('https://jsonplaceholder.typicode.com/users/1')
).json(),
}),
];
Now in every story, you can use the loader.currentUser value like this.
export const Primary = (args, { loaded: { currentUser } }) => (
<BlogItem {...args} {...currentUser} />
);
Loaders can be a super reliable way to set up reusable data that flows down to the excellent story without you having to do it repeatedly.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter