【发布时间】:2021-07-04 13:12:11
【问题描述】:
我不明白为什么我会收到此错误Property 'args' does not exist on type (args: Props) => Element
我正在尝试将 args 添加到我的 Storybook 组件中。这就是我的.stories.tsx 文件的样子
import React from "react";
import { Story, Meta } from "@storybook/react";
import { Props, Button } from ".";
export default {
title: "General/Button",
component: Button
} as Meta;
const Template = (args: Props) => <Button {...args} />;
export const PrimaryA = Template.bind({});
PrimaryA.args = { <-- ERROR
variant: "primary"
};
还有简单的.tsxButton组件文件
import { css } from "@emotion/react";
import React from "react";
export interface Props {
args: {
variant: string;
children?: React.ReactNode;
},
}
const style = css`
.primary {
background: #0082ff;
border-radius: 8px;
width: 150px;
height: 50px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
`;
export function Button(props: Props) {
const { variant = "primary", children = "Primary", ...rest } = props.args;
return (
<div css={style} className={`button ${variant}`} {...rest}>
{children}
</div>
);
}
你怎么看我已经在接口Props中有.args属性了。我不知道如何解决它。谢谢:))
编辑。
我编辑了界面
export interface Props {
variant: string;
children?: React.ReactNode;
}
还有PrimaryA对象
const Template = (props: Props) => <Button {...props} />;
export const PrimaryA = Template({
variant: "disabled"
});
还是什么都没有。我在 Storybook 中看不到组件
【问题讨论】:
-
Args是箭头函数'Template'的参数,不是Element的属性,所以报错是正确的
-
使用 PrimaryA = Template(args);而是
-
但是
Button作为元素也接收参数Button(props: Props) -
按钮接收 args 作为其 props 的属性(在 Props 接口中定义)。这并不意味着 args 是 Button 的属性
-
还是不行
标签: reactjs typescript storybook