Best practices for SwiftUI State Management
.cursorrules in your project root# SwiftUI State Management Rules
=====================================
## Core Principles
-------------------
1. **Separation of Concerns**: Keep view logic and state management separate to ensure maintainable and scalable code.
2. **Single Source of Truth**: Ensure that each piece of data has a single source of truth to avoid data inconsistencies.
3. **Immutable State**: Prefer immutable state to avoid unintended side effects and make debugging easier.
## Code Style Guidelines
-------------------------
### Naming
1. **State Variables**: Prefix state variables with `is`, `has`, or `should` to indicate their purpose (e.g., `isEditMode`, `hasError`, `shouldShowLoadingIndicator`).
2. **Action Handlers**: Use verb-based names for action handlers (e.g., `handleTap`, `handleSubmit`, `handleCancel`).
3. **View Models**: Suffix view models with `ViewModel` (e.g., `UserViewModel`, `ProductViewModel`).
### Typing
1. **Use Type Annotations**: Always use type annotations for state variables, action handlers, and view models to ensure clarity and prevent type-related errors.
2. **Use Optionals**: Use optionals for state variables that can be nil (e.g., `@State private var user: User?`).
3. **Use Enums**: Use enums for state variables that have a fixed set of possible values (e.g., `enum LoadingState { case idle, loading, error }`).
## Best Practices
------------------
1. **Use `@State` and `@StateObject`**: Use `@State` for simple state variables and `@StateObject` for more complex state management.
2. **Use `@EnvironmentObject`**: Use `@EnvironmentObject` to share state between views.
3. **Use `@Published`**: Use `@Published` to notify views of changes to state variables.
4. **Avoid Using `@State` in Child Views**: Avoid using `@State` in child views to prevent tight coupling and make state management more predictable.
5. **Use Action Handlers**: Use action handlers to handle user interactions and update state accordingly.
6. **Keep View Models Simple**: Keep view models simple and focused on a single responsibility to ensure maintainability.
## Common Pitfalls to Avoid
---------------------------
1. **Tight Coupling**: Avoid tight coupling between views and state management to ensure flexibility and maintainability.
2. **Over-Use of `@State`**: Avoid over-using `@State` to prevent unintended side effects and make debugging harder.
3. **Ignoring Immutable State**: Ignore immutable state to avoid unintended side effects and make debugging easier.
4. **Not Using Type Annotations**: Not using type annotations to ensure clarity and prevent type-related errors.
5. **Not Handling Errors**: Not handling errors to ensure robustness and prevent crashes.
6. **Over-Complex View Models**: Over-complex view models to ensure maintainability and scalability.Comprehensive Cursor rules for Next.js 14+ with App Router, including routing, layouts, and API patterns.
Cursor rules for TypeScript with strict type checking, advanced patterns, and best practices.
Cursor rules for Tailwind CSS development with responsive design, custom components, and dark mode.
Cursor
coding
AI coding rules customize how Cursor generates and refactors code for your project. Follow these steps to install SwiftUI State Management.
.cursor/rules, for Windsurf use .windsurfrulesComprehensive Cursor rules for Next.js 14+ with App Router, including routing, layouts, and API patterns.
Cursor rules for TypeScript with strict type checking, advanced patterns, and best practices.
Cursor rules for Tailwind CSS development with responsive design, custom components, and dark mode.