Skip to content
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

Loading external data before adding story #696

Closed
dnlsandiego opened this issue Feb 16, 2017 · 3 comments
Closed

Loading external data before adding story #696

dnlsandiego opened this issue Feb 16, 2017 · 3 comments

Comments

@dnlsandiego
Copy link

dnlsandiego commented Feb 16, 2017

Is it possible to do some async work like fetching of data via external API before rendering/adding a story?

Kinda like this inside:

// inside src/stories/index.js

axios.get('http://api.someURLtogetdata.com')
  .then(({ data }) => {
    storiesOf('Data Viewer', module)
      .add('Test', () => {
        return <TestStory data={data} />;
      })
});

This doesn't work. I also tried doing something like this:

const stories = storiesOf('Data Viewer', module)

axios.get('http://api.someURLtogetdata.com')
  .then(({ data }) => {
      stories.add('Test', () => {
        return <TestStory data={data} />;
      })
});

Any tips on how to achieve something like this with storybook?

@einarlove
Copy link

Could suggest something like

class LoadData = extends React.Component {
  state = {
    data: null
  }

  componentDidMount() {
    axios.get(this.props.url).then(data => this.setState({ data }))
  }

  render() {
    return this.props.children(this.state.data)
  }
}

storiesOf('DataViewer')
  .add('test', () => (
    <LoadData url="http://api.someURLtogetdata.com">
      {data => 
        <TestStory data={data} />
      }
    </LoadData>
  ))

@ndelangen
Copy link
Member

Thank you for the tip @einarlove!

This issue hints at the same problem and has a possible solution: #713

What do you feel about it?

I imagine the API would look like this (this is not implemented):

storiesOf('DataViewer')
  .add('test', () => axios
    .get('http://api.someURLtogetdata.com')
    .then(({ data }) => <TestStory data={data} />)
  )

@ndelangen
Copy link
Member

I would like to hear your feedback! If you think the solution I just posted would be fine for your usecase, let's talk about it in this issue: #713.

If you disagree and want another API, you're welcome to re-open this issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants