DEV Community

CoderFeri
CoderFeri

Posted on

Mastering Custom Components in HarmonyOS NEXT

Mastering Custom Components in HarmonyOS NEXT

I'm Feri, a programmer with over 12 years of experience, having been involved in development projects, led teams, and even ventured into entrepreneurship. Proficient in Java, embedded systems, HarmonyOS, artificial intelligence, and more, I'm dedicated to the growth of programmers. I look forward to walking this journey alongside you! Where there is a will, there is a way!

In the development world of HarmonyOS NEXT, have you ever been frustrated by repeatedly writing similar UI interfaces? Do you long for the "code magic" that allows you to combine interface elements as freely as building blocks? Don't worry! Today, I'll guide you to unlock the "magic weapon" of custom components, enabling you to effortlessly build personalized UIs and bid farewell to reinventing the wheel!

1. Custom Components: Your Exclusive UI "Magic Factory"

On the stage of ArkUI, all UI elements are components. System components are like standardized parts mass-produced by a factory, while custom components are unique crafts you create with your own hands. Compared with the lightweight @builder reuse mechanism, custom components are like a "super factory." They can not only reuse UI but also encapsulate complex business logic, making them an essential skill for improving development efficiency in daily work.

2.Grammar Basics: Three Steps to Set Up a Custom Component "Production Line"

The grammar of custom components isn't overly complex. Remember these three key elements, and you can easily set up your own "production line":

@Entry
@Component
struct Index {
  build() {

Enter fullscreen mode Exit fullscreen mode

Here, the @Entry is the "key to the UI page's door," the @Component decorator labels the component as "customized," and the build() function serves as the "design blueprint" that describes what the component looks like.

3.Hands-On Operation: Creating Your First Custom Component from Scratch

Curious about how to create a custom component? Follow these steps, and you'll have your very own component in no time:
Remember, the name of a custom component cannot be the same as that of a system component, just as everyone needs a unique ID.
Tips for Component Creation
struct: The "skeleton" of a custom component, implemented based on struct. You can instantiate it without using new, which is both concise and efficient.
@Component: The exclusive "customization badge" that can only decorate data structures declared with struct, marking it as a custom component.

build() function: The core "design blueprint" that must be defined, used to describe the UI layout of the component.

@Entry: The "storefront sign" of the page. There can be at most one per page, determining where the user interface starts.

@Preview: The component's "fitting room." Use it to preview the component's effect separately, making it convenient to adjust the design.

4.Practice Makes Perfect: Creating Personalized Components and Mastering Parameter Passing

Theory alone isn't enough. Let's roll up our sleeves and practice! Try creating and using custom components, and explore different ways to use them:
Reference Example 1: A Simple Interactive Component

Reference Example 2: File-Splitting Management
Separate component definition and usage to make the code neater:
HelloComponent.ets
Page.ets

5.Advanced Techniques: Adding a "Brain" to Components

Custom components aren't just about looks; they can also be "intelligent"! Besides the build() function, you can add member functions and variables:
However, note that static functions and variables aren't supported here, and all member functions and variables are private, like the component's "personal secrets."
Practical Case: An Intelligent Interactive Component

When using it, you can also pass parameters to overwrite default values or even pass in custom functions:

6.Beautifying Components: Creating "Gorgeous Looks" with Styles and Events

Custom components can also be stunningly beautiful! You can easily set common styles and events using dot notation:

However, it's important to note that styles are actually applied to an invisible container outside the component. Take this example:

You'll notice that the red background isn't directly applied to the Button but rather to the invisible container wrapping it. Mastering this feature allows you to control the component's appearance more precisely!

Now, you've grasped the core techniques of custom components in HarmonyOS NEXT! From basic creation to advanced applications, these skills empower you to shine in development. Roll up your sleeves and start creating your personalized components! If you encounter any issues or have new discoveries during practice, feel free to share them in the comments. Let's explore more "code magic" together!

Top comments (0)