【发布时间】:2020-04-29 22:07:05
【问题描述】:
我正在尝试为 Next.js 项目设置 Storybook。我有一个从 Next.js 呈现 Link 标记的组件。我的问题是,当我加载这个组件时,Storybook 会抛出以下错误:
Cannot read property 'pageLoader' of null
at Link.handleRef
要让 Storybook 使用 Next.js 路由,特别是渲染 Link 标记,需要做什么?
更新:导致错误的代码:
// button-component.js
import Link from 'next/link.js';
import t from 'prop-types';
import React from 'react';
function Button({ as, children, href, ...props }) {
const isExternal = href && href.startsWith('http');
const a = (
<a href={href} {...props}>
{children}
</a>
);
if (href) {
return isExternal ? (
a
) : (
<Link href={href} as={as}>
{a}
</Link>
);
}
return (
<button type="button" {...props}>
{children}
</button>
);
}
Button.propTypes = {
as: t.string,
children: t.node,
href: t.string,
};
export default React.memo(Button);
// button.stories.js
import React from 'react';
import Button from './button-component';
export default {
title: 'Button',
};
export const standardButton = () => <Button>Standard Button</Button>;
export const internalLink = () => <Button href='/buy'>
Internal Link
</Button>;
export const externalLink = () => (
<Button target="_blank" href="https://www.hopin.to">
External Link
</Button>
);
【问题讨论】:
-
你能贴一些代码吗?
-
下一个版本是什么?
-
@Cully 当然,完成!
-
@felixmosh Next.js:“9.1.7”,故事书:“5.3.1”
-
你能设置一个小仓库来重现这个问题吗?