1. Software architecture

MVVM (model-view-viewmodel)

Both SwiftUI and React comform to MVVM pattern.


2. View vs Component

2.1 lifecycle

View lifecycle is managed via viewmodifiers and init func as below.

Component lifecycle is managed via hook as below.

useEffect({
/* dependants array is empty, this only execute once at beginning for init*/
},[]);

useEffect({
    // this block will be executed during each frame update 
    // mainly used for animation
});

useEffect({
    // here will be executed only if dependants array changed
    return ()->{
        // do some clean jobs
    } // this closure will be executed before next component update
}, [dependant])

2.2 @State vs useState


3. Value passing

It’s an important topic for value passing between differect blocks. Value passing can be classified into 4 types.

3.1 parent -> child

3.2 child -> parent

3.3 brother -> brother

3.4 any -> any


4. Global State management

Usually, a global state manager plays a role of viewModel.

4.1 State management in SwiftUI

In SwiftUI, we can use pulisher and subscriber pattern to implement state management.

@EnvironmentObject is more global than @ObservedObject, @ObservedObject cannot be shared without props drilling. @EnvironmentObject is totally free to use in different Views after defined somewhere once.

Moreover, in swiftUI already predefined a lot of @Environment states for users to retrieve directly. (eg. \.colorScheme, \.locale, \.calendar and more)

4.2 State management in React

In React world, there are many 3rd party frameworks to manage global states. such as Redux, MobilX, Zusland, Valtio etc.

useContext is not a real global state management, it only works in Component realm.

Recommend book about mircostate management in react: Micro State Management with React Hooks: Explore custom hooks libraries like Zustand, Jotai, and Valtio to manage global states


5. Performance

Both SwiftUI and React render update frames continuously. Thus States and events will cause tons of redraw calls, that heavily affects app’s performance. So it should be carefully considered when and how will cause a draw call by States and events. Reducing unneccessery draw call is very important.