TypeScript Type Definitions
When using TypeScript, we can define types for component properties:
tsx
interface BoxProps {
style?: {
position?: { offset: Vec2 };
size?: { offset: Vec2 };
};
children?: any;
}
function Box({ style, children }: BoxProps) {
return <box style={style}>{children}</box>;
}
Conditional Rendering
In addition to the ternary operator, we can also use the following methods for conditional rendering:
1. Logical AND Operator
tsx
<box>{isLoggedIn && <text>Welcome back</text>}</box>
2. Conditional Function
tsx
function renderContent(isLoading: boolean) {
if (isLoading) {
return <text>Loading...</text>;
}
return <text>Content loaded</text>;
}
<box>{renderContent(isLoading)}</box>;
List Rendering
When using the map
function to render a list, pay attention to the following points:
tsx
const items = [
{ id: 1, name: "Item 1" },
{ id: 2, name: "Item 2" },
{ id: 3, name: "Item 3" },
];
<box>
{items.map((item) => (
<text
style={{ position: { offset: Vec2.create({ x: 0, y: item.id * 30 }) } }}
>
{item.name}
</text>
))}
</box>;
Effect:
Performance Optimization Suggestions
- Avoid Unnecessary Re-renders
- Wrap purely presentational components with
React.memo
. - Use
useMemo
anduseCallback
appropriately.
- Wrap purely presentational components with
- Style Optimization
- Extract static styles into constants.
- Event Handling Optimization
- Avoid creating new functions during rendering.
- Cache event handler functions with
useCallback
.
- List Rendering Optimization
- Use virtual lists for handling large amounts of data.
- Implement
shouldComponentUpdate
or useReact.memo
.
Best Practices
- Location of Type Definitions
- Place type definitions in separate type files.
- Use
type
orinterface
depending on the specific situation. - Prefer
interface
for extension.
- Type Naming
- Use PascalCase for naming types.
- Use meaningful type names.
- Avoid using overly generic type names.
- Type Reusability
- Extract common types into shared files.
- Use type utilities to reduce duplicate code.
- Use type aliases reasonably.
- Type Safety
- Use strict type checking.
- Avoid using the
any
type. - Use type guards to ensure type safety.
- Performance Considerations
- Avoid deeply nested types.
- Use type utilities to optimize complex types.
- Be aware of the performance impact of type inference.