Skip to Content
React Cookbook 中文版:87 个案例带你精通React 框架
book

React Cookbook 中文版:87 个案例带你精通React 框架

by David Griffiths, Dawn Griffiths
March 2023
Intermediate to advanced
468 pages
9h 32m
Chinese
China Machine Press
Content preview from React Cookbook 中文版:87 个案例带你精通React 框架
30
|
1
你可以从 GitHub 网站(
https://oreil.ly/aB7BP
)下载本解决方案的源代码。
1.9 在组件开发中使用 Storybook
问题
React 应用程序是用一个个 React 组件搭建而成的。如果组件编写得当,我们是可以在
其他 React 应用程序中轻松复用这些组件的。但创建组件后,检查组件在其他情况下
是否也能正常工作并不容易。例如,在异步应用程序中,React 组件在运行时接收到
的属性可能是 undefined(未定义的)。此时这些组件仍然能正确显示吗?它会显示错
误吗?
但是,如果你在为一个复杂的应用程序创建组件,那么你的组件很难适用于所有情况。
此外,如果你团队中的用户体验(UX)开发人员
如果有的话
查看特定组件时还
需要在应用程序中到处翻找,那么可能会比较浪费时间。
如果有种方法可以将组件单独集中展示,并且还可以提供一些组件的属性示例,那么将
会很有帮助。
解决方案
Storybook 是一个用于展示不同状态下 UI 组件的工具。你可以把它当成组件状态的管理
器。实际上,Storybook 也是可以用作组件开发的工具。
如何将 Storybook 添加到项目中呢?首先用
create-react-app
创建一个 React 应用程序:
$ npx create-react-app my-app
$ cd my-app
现在我们可以把 Storybook 添加到项目中:
$ npx sb init
然后我们用
yarn
npm
启动 Storybook 服务器:
$ npm ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Vue.js

Vue.js

Fabian Deitelhoff
MobX Quick Start Guide

MobX Quick Start Guide

Pavan Podila, Michel Weststrate

Publisher Resources

ISBN: 9787111722809