# Custom Component Initialization
This topic describes the rules for initializing component state variables.
The member variables of a component can be initialized in either of the following ways:
- Local initialization. For example:
```
@State counter: Counterr = new Counter()
```
- Using constructor parameters for initialization. For example:
```
MyComponent(counter: $myCounter)
```
The allowed method depends on the decorator of the state variable.
Decorator Type
|
Local Initialization
|
Using Constructor Parameters for Initialization
|
@State
|
Mandatory
|
Optional
|
@Prop
|
Forbidden
|
Mandatory
|
@Link
|
Forbidden
|
Mandatory
|
@StorageLink
|
Mandatory
|
Forbidden
|
@StorageProp
|
Mandatory
|
Forbidden
|
@Provide
|
Mandatory
|
Optional
|
@Consume
|
Forbidden
|
Forbidden
|
@ObjectLink
|
Forbidden
|
Mandatory
|
Normal member variable
|
Recommended
|
Optional
|
In the preceding table:
- The **@State** annotated variable needs to be initialized locally. The initial value can be overwritten by the constructor parameter.
- The **@Prop** and **@Link** annotated variables must be initialized only by constructor parameters.
Comply with the following rules when using constructors to initialize member variables:
From the Variable in Parent Component (Below) to the Variable in Child Component (Right)
|
@state
|
@Link
|
@Prop
|
Normal Variable
|
@State
|
Allowed
|
Allowed
|
Allowed
|
Allowed
|
@Link
|
Not allowed
|
Allowed
|
Not recommended
|
Allowed
|
@Prop
|
Not allowed
|
Not allowed
|
Allowed
|
Allowed
|
@StorageLink
|
Not allowed
|
Allowed
|
Not allowed
|
Allowed
|
@StorageProp
|
Not allowed
|
Not allowed
|
Not allowed
|
Allowed
|
Normal variable
|
Allowed
|
Not allowed
|
Not allowed
|
Allowed
|
In the preceding table:
- The normal variable of the parent component can be used to initialize the **@State** annotated variable of the child component, but cannot be used to initialize the **@Link** or **@Prop** annotated variable.
- The **@State** annotated variable of the parent component can initialize the **@Prop**, **@Link** \(using **$**\), or normal variables of the child component, but cannot initialize the **@State** annotated variable of the child component.
- The **@Link** annotated variable of the parent component can initialize the **@Link** annotated or common variables of the child component. However, it is a syntax error to initialize the **@State** annotated member of the child component. In addition, initializing the **@Prop** annotated variable is not recommended.
- The **@Prop** annotated variable of the parent component can initialize the normal variables or **@Prop** annotated variables of its child components, but cannot initialize the **@State** or **@Link** annotated variables.
- **@StorageLink** and **@StorageProp** cannot be passed from the parent component to its child components.
- In addition to the preceding rules, the TypeScript strong type rules need to be followed.
## Example
```
@Entry
@Component
struct Parent {
@State parentState: ClassA = new ClassA()
build() {
Row() {
CompA({aState: new ClassA, aLink: $parentState}) // valid
CompA({aLink: $parentState}) // valid
CompA() // invalid, @Link aLink remains uninitialized
CompA({aLink: new ClassA}) // invalid, @Link aLink must be a reference ($) to either @State or @Link variable
}
}
}
@Component
struct CompA {
@State aState: boolean = false // must initialize locally
@Link aLink: ClassA // must not initialize locally
build() {
Row() {
CompB({bLink: $aLink, // valid init a @Link with reference of another @Link,
bProp: this.aState}) // valid init a @Prop with value of a @State
CompB({aLink: $aState, // invalid: type missmatch expected ref to ClassA, provided reference to boolean
bProp: false}) // valid init a @Prop by constants value
}
}
}
@Component
struct CompB {
@Link bLink: ClassA = new ClassA() // invalid, must not initialize locally
@Prop bProp: boolean = false // invalid must not initialize locally
build() {
...
}
}
```