0Pricing
Design Systems & Component Libraries · 课时

在应用中使用组件

学习如何在不同的应用场景和框架中,有效导入并使用设计系统中的组件

在应用中使用组件 是 CoddyKit 上的免费 Design Systems & Component Libraries 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Design Systems & Component Libraries 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Design Systems & Component Libraries 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Welcome to Component Consumption!

So you've got a shiny new design system! But how do you actually use those beautiful, pre-built components in your apps?

This lesson will show you how to integrate and utilize components from a design system, making your development process smoother and your UIs more consistent.

The Power of Reusability

Consuming design system components isn't just about making your app look good; it's about efficiency and consistency.

  • Speed: Build features faster by using pre-made UI blocks.
  • Consistency: Ensure a unified look and feel across all your products.
  • Quality: Components are often tested and accessible by default.
  • Maintainability: Updates to the design system propagate easily.

How Components Are Delivered

Design system components are typically published as packages, similar to other libraries you might use.

These packages are usually distributed via a package manager like npm (Node Package Manager) or Yarn. This allows developers to easily install and update them.

Think of it like an app store for code!

Getting Your Design System Ready

Before you can use components, you need to install the design system package into your project. This is usually a simple command in your terminal.

For example, if your design system components are published under @my-ds/components, you'd run:

npm install @my-ds/components

This command downloads the component library and makes it available to your application.

Bringing Components into Your Code

Once installed, you can import specific components into your application's files. This makes them available for use in your UI.

Imagine you have a Button and a Card component:

import Button from '@my-ds/button';
import Card from '@my-ds/card';

function main() {
  console.log("Components imported successfully!");
  console.log("Ready to use Button and Card in your UI.");
}

main();

Placing Components on the Screen

After importing, you can use the components directly in your application's UI code (e.g., JSX in React, templates in Vue/Angular).

Components often accept props (properties) to customize their appearance or behavior.

import Button from '@my-ds/button';

function renderApp() {
  // In a real app, this would return UI elements.
  // Here, we simulate rendering a primary button.
  const primaryButton = `<button class="ds-button ds-button--primary">Submit</button>`;
  console.log("Rendered a primary button (conceptually):");
  console.log(primaryButton);

  // And a secondary button
  const secondaryButton = `<button class="ds-button ds-button--secondary">Cancel</button>`;
  console.log("Rendered a secondary button (conceptually):");
  console.log(secondaryButton);
}

function main() {
  renderApp();
}

main();

Making Components Interactive

Many components respond to user interactions like clicks, input changes, or hovers. You can attach event handlers to run your own code when these events occur.

This allows your application to react to user actions and update its state.

import Button from '@my-ds/button';

function handleClick() {
  console.log("Button was clicked! Action performed.");
}

function renderApp() {
  // Imagine this button has an onClick prop that calls handleClick
  const interactiveButton = `<button class="ds-button" onclick="handleClick()">Click Me</button>`;
  console.log("Simulating a button click:");
  handleClick(); // Call the handler directly to show its effect
  console.log("Interactive button (conceptually):", interactiveButton);
}

function main() {
  renderApp();
}

main();

How Styles Are Applied

Design system components come with their own styles. How these styles are applied depends on the system:

  • Global CSS: Styles are loaded once for the whole app.
  • CSS-in-JS: Styles are coupled directly with components.
  • Utility classes: Components might expose classes for further customization.

Often, you just import a main stylesheet once, or styles are automatically included with the components themselves.

Global Settings for Components

Some design systems use Context or Providers to manage global settings like themes (light/dark mode), language, or user preferences.

You might wrap your application with a ThemeProvider component to make theme settings available to all components within it, without passing props repeatedly.

Test Your Knowledge

You've learned how to bring design system components into your application and use them.

Which of the following commands is typically used to install a design system component library published as an npm package?

Consuming Components: A Summary

Great job! You've learned the essentials of consuming design system components:

  • Installing component packages via npm/Yarn.
  • Importing components into your application.
  • Using components with props and event handlers.
  • Understanding how styles and global contexts work.

By leveraging design system components, you build better, more consistent UIs faster. Next, we'll look at handling overrides and customizations.

常见问题解答

「在应用中使用组件」课时是免费的吗?

是的 — 「在应用中使用组件」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Design Systems & Component Libraries 课程的其余内容,请升级到 CoddyKit PRO。 Design Systems & Component Libraries 课程共包含 4 节课。

「在应用中使用组件」这节课中我会学到什么?

学习如何在不同的应用场景和框架中,有效导入并使用设计系统中的组件 你通过在浏览器中直接运行的动手代码来练习 Design Systems & Component Libraries,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Design Systems & Component Libraries 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Design Systems & Component Libraries 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「在应用中使用组件」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Design Systems & Component Libraries 课中编写并运行代码吗?

能。每节 Design Systems & Component Libraries 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 在应用中使用组件
  2. 处理覆盖与自定义
  3. 产品迁移策略
  4. 面向使用方的版本管理与依赖更新
← 返回 Design Systems & Component Libraries