故事书:渲染异步组件

时间:2019-09-26 08:19:35

标签: reactjs storybook

我有一个<AsyncComponent />可以懒加载组件:

class AsyncComponent extends Component {
  state = {
    Cmp: null
  };

  componentDidMount() {
    this.getComponent();
  }

  getComponent = async () => {
    if (!this.state.Cmp) {
      try {
        const { default: Cmp } = await this.props.moduleProvider();
        this.setState({ Cmp });
      } catch (e) {
        /* Handle failure to load dynamic component */
      }
    }
  };

  render(props, { Cmp}) {
    return Cmp ? <Cmp {...props} /> : 'Loading';
  }
}

它的故事就像这样:

import AsyncComponent from "./AsyncComponent";

storiesOf("AsyncComponent", module).add("default", () => (
  <AsyncComponent moduleProvider={() => ({ default: <h1>Hello world</h1> })} />
));

但是我在故事书上什么都没看到...

0 个答案:

没有答案
相关问题