# 開發組件庫時的問題
- create-react-app 入口文件不適合管理組件庫(測試時)
- 缺少行為追蹤和屬性調試功能
組件完美開發工具應有的特點:
- 分開展示各個組件不同屬性下的狀態
- 能追蹤組件的行為并且具有屬性調試功能
- 可以為組件自動生成文檔和屬性列表
# Storybook 的使用(React)
[https://storybook.js.org/](https://storybook.js.org/)
下面是我的版本(2020.6.11)
```
"devDependencies": {
"@storybook/addon-actions": "^5.3.19",
"@storybook/addon-links": "^5.3.19",
"@storybook/addons": "^5.3.19",
"@storybook/preset-create-react-app": "^3.0.0",
"@storybook/react": "^5.3.19"
}
```
不同的項目搭建方式安裝方法不同:[https://storybook.js.org/docs/guides/guide-react/](https://storybook.js.org/docs/guides/guide-react/)
因為本項目是 CRA + TypeScript,所以使用如下方式安裝:
```
npx -p @storybook/cli sb init --type react_scripts
```
注意:如果你之前用 cnpm 安裝了其他包很可能會報錯,這時候把 node_modules 整個文件夾刪了,執行 npm i 重新安裝一次。
這樣安裝就已經支持 typescript 了,省去了很多麻煩。
[https://blog.csdn.net/yehuozhili/article/details/106044494](https://blog.csdn.net/yehuozhili/article/details/106044494)
## 配置文件
```
// .storybook/main.js
module.exports = {
stories: ['../src/**/*.stories.tsx'],
addons: [
'@storybook/preset-create-react-app',
'@storybook/addon-actions',
'@storybook/addon-links',
]
};
```
```
// .storybook/preview.js 加載樣式
import { configure } from '@storybook/react'
import "../src/styles/index.scss"
configure(require.context('../src', true, /\.stories\.tsx$/), module)
```
## 書寫方式
[https://storybook.js.org/basics/writing-stories/](https://storybook.js.org/basics/writing-stories/)
有 CSF、classic storiesOf API、MDX syntax 三種方式,這里用比較經典的 classic 方式,因為 CSF 函數就是故事名,而中文文檔函數名是中文是很不好的。
```
import React from 'react'
import { action } from '@storybook/addon-actions'
import { storiesOf } from '@storybook/react'
import Button from './button'
const defaultButton = () => {
return (
<Button onClick={action('clicked')}>default Button</Button>
)
}
const buttonWithSize = () => (
<>
<Button size="lg"> large button </Button>
<Button size="sm"> large button </Button>
</>
)
const buttonWithType = () => (
<>
<Button btnType="primary"> primary button </Button>
<Button btnType="danger"> danger button </Button>
<Button btnType="link" href="https://google.com"> link button </Button>
</>
)
storiesOf('Button Component', module)
.add('默認 Button', defaultButton)
.add('不同尺寸的 Button', buttonWithSize)
.add('不同類型的 Button', buttonWithType)
```
## 插件介紹
[https://storybook.js.org/addons/](https://storybook.js.org/addons/)
安裝插件后 main.js 導入,形式如下:
```
module.exports = {
stories: ['../src/**/*.stories.tsx'],
addons: [
'@storybook/preset-create-react-app',
'@storybook/addon-actions',
'@storybook/addon-links',
]
};
```
可以直接在單個 stories 里使用,也可以在全局的配置 preview.js 里使用(應用到所有的 stories)。
### addon-info 的使用
[https://github.com/storybookjs/storybook/tree/master/addons/info](https://github.com/storybookjs/storybook/tree/master/addons/info)
***
addon-info 是較常用的一個插件,安裝:
```
npm i -D @storybook/addon-info
```
or
```
yarn add @storybook/addon-info --dev
```
其沒有自帶 types,需要另行安裝:
```
npm install --save @types/storybook__addon-info
```
使用 yarn 安裝出現 node_sass 不匹配問題(Windows),主要是卡在 waiting 后報錯。
```
yarn global add node-gyp windows-build-tools
```
### react-docgen 的使用
***
其主要就是用于生成組件的屬性表格。
storybook 自帶,但是支持 TS 的話需要另外安裝 loader:
[https://github.com/strothj/react-docgen-typescript-loader](https://github.com/strothj/react-docgen-typescript-loader)
```
$ npm install --save-dev react-docgen-typescript-loader
or
$ yarn add --dev react-docgen-typescript-loader
```
需要配置 webpack:
```
const path = require('path')
module.exports = ({ config }) => {
config.module.rules.push({
test: /\.tsx?$/,
use: [
{
loader: require.resolve("babel-loader"),
options: {
presets: [require.resolve("babel-preset-react-app")]
}
},
{
loader: require.resolve("react-docgen-typescript-loader"),
options: {
shouldExtractLiteralValuesFromEnum: true,
tsconfigPath: path.resolve(__dirname, "../tsconfig.json"),
propFilter: (prop) => {
if (prop.parent) {
return !prop.parent.fileName.includes('node_modules')
}
return true
}
}
}
]
});
config.resolve.extensions.push(".ts", ".tsx");
return config;
};
```
有兩個 bug 需要注意:
[https://github.com/strothj/react-docgen-typescript-loader#limitations](https://github.com/strothj/react-docgen-typescript-loader#limitations)
1、When extending from React.Component as opposed to Component, docgens don't seem to be created. Ref issue #10 (thanks @StevenLangbroek for tracking down the cause).
Doesn't work:
```
import React from 'react';
interface IProps {
whatever?: string;
};
export default class MyComponent extends React.Component<IProps> {}
```
Works:
```
import React, { Component } from 'react';
export default class MyComponent extends Component<IProps> {}
```
就是說代碼里使用`TS.Component`的形式都換成先 import 再使用。
<br/>
2、Export Names
Component docgen information can not be generated for components that are only exported as default. You can work around the issue by exporting the component using a named export.
```
import * as React from "react";
interface ColorButtonProps {
/** Buttons background color */
color: "blue" | "green";
}
/** A button with a configurable background color. */
export const ColorButton: React.SFC<ColorButtonProps> = props => (
<button
style={{
padding: 40,
color: "#eee",
backgroundColor: props.color,
fontSize: "2rem",
}}
>
{props.children}
</button>
);
export default ColorButton;
```
- 序言 & 更新日志
- H5
- Canvas
- 序言
- Part1-直線、矩形、多邊形
- Part2-曲線圖形
- Part3-線條操作
- Part4-文本操作
- Part5-圖像操作
- Part6-變形操作
- Part7-像素操作
- Part8-漸變與陰影
- Part9-路徑與狀態
- Part10-物理動畫
- Part11-邊界檢測
- Part12-碰撞檢測
- Part13-用戶交互
- Part14-高級動畫
- CSS
- SCSS
- codePen
- 速查表
- 面試題
- 《CSS Secrets》
- SVG
- 移動端適配
- 濾鏡(filter)的使用
- JS
- 基礎概念
- 作用域、作用域鏈、閉包
- this
- 原型與繼承
- 數組、字符串、Map、Set方法整理
- 垃圾回收機制
- DOM
- BOM
- 事件循環
- 嚴格模式
- 正則表達式
- ES6部分
- 設計模式
- AJAX
- 模塊化
- 讀冴羽博客筆記
- 第一部分總結-深入JS系列
- 第二部分總結-專題系列
- 第三部分總結-ES6系列
- 網絡請求中的數據類型
- 事件
- 表單
- 函數式編程
- Tips
- JS-Coding
- Framework
- Vue
- 書寫規范
- 基礎
- vue-router & vuex
- 深入淺出 Vue
- 響應式原理及其他
- new Vue 發生了什么
- 組件化
- 編譯流程
- Vue Router
- Vuex
- 前端路由的簡單實現
- React
- 基礎
- 書寫規范
- Redux & react-router
- immutable.js
- CSS 管理
- React 16新特性-Fiber 與 Hook
- 《深入淺出React和Redux》筆記
- 前半部分
- 后半部分
- react-transition-group
- Vue 與 React 的對比
- 工程化與架構
- Hybird
- React Native
- 新手上路
- 內置組件
- 常用插件
- 問題記錄
- Echarts
- 基礎
- Electron
- 序言
- 配置 Electron 開發環境 & 基礎概念
- React + TypeScript 仿 Antd
- TypeScript 基礎
- React + ts
- 樣式設計
- 組件測試
- 圖標解決方案
- Storybook 的使用
- Input 組件
- 在線 mock server
- 打包與發布
- Algorithm
- 排序算法及常見問題
- 劍指 offer
- 動態規劃
- DataStruct
- 概述
- 樹
- 鏈表
- Network
- Performance
- Webpack
- PWA
- Browser
- Safety
- 微信小程序
- mpvue 課程實戰記錄
- 服務器
- 操作系統基礎知識
- Linux
- Nginx
- redis
- node.js
- 基礎及原生模塊
- express框架
- node.js操作數據庫
- 《深入淺出 node.js》筆記
- 前半部分
- 后半部分
- 數據庫
- SQL
- 面試題收集
- 智力題
- 面試題精選1
- 面試題精選2
- 問答篇
- 2025面試題收集
- Other
- markdown 書寫
- Git
- LaTex 常用命令
- Bugs