question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding stories from data structure throws error

See original GitHub issue

I have a working set of stories from the sample code. However, when I try to populate stories from a simple data structure, storybook gives me an error. Loading stories from data would keep code DRY when you are sharing fixture data between stories and unit tests.

When I modify the storybook example components/stories/TodoItem.js with the following trivial change, it gives me the following error preview.js?a230:61 Uncaught TypeError: story is not a function. Is there a better way to do this?

const data = {
  'not completed': { id: 'the-id', text: 'Hello Todo', completed: false },
  'completed': { id: 'the-id', text: 'Hello Todo', completed: true }
}

const todoStories = storiesOf('TodoItem', module)
for (const key in data) {
  todoStories.add(key, getItem(data[key]))
}

// storiesOf('TodoItem', module)
//   .add('not completed', () => {
//     const todo = {
//       id: 'the-id',
//       text: 'Hello Todo',
//       completed: false
//     };
//
//     return getItem(todo);
//   })
//   .add('completed', () => {
//     const todo = {
//       id: 'the-id',
//       text: 'Hello Todo',
//       completed: true
//     };
//
//     return getItem(todo);
//   });

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
thani-shcommented, Apr 14, 2016

Hi @shilman, I believe the code given above will work fine if we use an arrow function.

const todoStories = storiesOf('TodoItem', module)
for (const key in data) {
  todoStories.add(key, () => getItem(data[key]))
}

React storybook can be modified to support data structures by changing these 2 lines.

But using a function to create the component can be useful. Let’s keep the issue open for now.

1reaction
jeef3commented, Apr 14, 2016

Could be a convenience function perhaps, something like todoStories.addAll(data)?

I was thinking this as well. The only problem though is that currently the add() story requires you to pass a function that does the rendering. addAll could only work like you’ve described if a Story was set-up knowing what component it would render, and then only allowed you to pass props.

Perhaps something like:

todoStories.addAll(data, (props) => <TodoItem {...props} />);

Where the render function of addAll passes the props each time. Could actually work quite well with functional components:

// functional component
const TodoItem = ({ id, text, completed }) => <div>{id}: {text} {completed ? 'Y' : 'N'}</div>;

// ... other set-up

// Since it would be called, given the props.
todoStories.addAll(data, TodoItem);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding stories from data structure throws error · Issue #112
I have a working set of stories from the sample code. However, when I try to populate stories from a simple data structure,...
Read more >
Error when trying to add data to vector inside structure
I am trying to add some data from a . txt file into a vector which is inside a structure, but the vector...
Read more >
Handling operation errors - Apollo GraphQL Docs
By default, Apollo Client throws away partial data and populates the error.graphQLErrors array of your useQuery call (or whichever hook you're using).
Read more >
Saving Data | Firebase Realtime Database - Google
Ways to Save Data ; push, Add to a list of data in the database. Every time you push a new node onto...
Read more >
How to Add Extra Information to Errors in Go - DigitalOcean
One feature is the ability to wrap errors using the fmt.Errorf function with an error value that can be unwrapped later to access...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found