Redux-like state container in SwiftUI. Basics.

The main idea here is describing the whole app state by using a single struct or composition of structs. Assume that we are working on a Github repos search app where the state is the repos array that we fetch matching some query using Github API.

s…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Sergey Leschev

The main idea here is describing the whole app state by using a single struct or composition of structs. Assume that we are working on a Github repos search app where the state is the repos array that we fetch matching some query using Github API.

struct AppState {
    var searchResult: [Repo] = []
}

Next step is passing the read-only app state to every view inside the app. The best way for doing that is SwiftUI’s Environment feature. We can put an object holding the whole app state in the Environment of the root view. Root view will share its Environment with all child views.

final class Store: ObservableObject {
    @Published private(set) var state: AppState
}

We create a store object that stores the app state and provides read-only access to it. State property uses @Published property wrapper that notifies SwiftUI during any changes. It allows us to keep up to date the whole app by deriving it from a single source of truth.

Contacts
I have a clear focus on time-to-market and don't prioritize technical debt. And I took part in the Pre-Sale/RFX activity as a System Architect, assessment efforts for Mobile (iOS-Swift, Android-Kotlin), Frontend (React-TypeScript) and Backend (NodeJS-.NET-PHP-Kafka-SQL-NoSQL). And I also formed the work of Pre-Sale as a CTO from Opportunity to Proposal via knowledge transfer to Successful Delivery.

🛩️ #startups #management #cto #swift #typescript #database
📧 Email: sergey.leschev@gmail.com
đź‘‹ LinkedIn: https://www.linkedin.com/in/sergeyleschev/
đź‘‹ LeetCode: https://leetcode.com/sergeyleschev/
đź‘‹ Twitter: https://twitter.com/sergeyleschev
đź‘‹ Github: https://github.com/sergeyleschev
🌎 Website: https://sergeyleschev.github.io


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Sergey Leschev


Print Share Comment Cite Upload Translate Updates
APA

Sergey Leschev | Sciencx (2023-02-11T15:33:51+00:00) Redux-like state container in SwiftUI. Basics.. Retrieved from https://www.scien.cx/2023/02/11/redux-like-state-container-in-swiftui-basics/

MLA
" » Redux-like state container in SwiftUI. Basics.." Sergey Leschev | Sciencx - Saturday February 11, 2023, https://www.scien.cx/2023/02/11/redux-like-state-container-in-swiftui-basics/
HARVARD
Sergey Leschev | Sciencx Saturday February 11, 2023 » Redux-like state container in SwiftUI. Basics.., viewed ,<https://www.scien.cx/2023/02/11/redux-like-state-container-in-swiftui-basics/>
VANCOUVER
Sergey Leschev | Sciencx - » Redux-like state container in SwiftUI. Basics.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/11/redux-like-state-container-in-swiftui-basics/
CHICAGO
" » Redux-like state container in SwiftUI. Basics.." Sergey Leschev | Sciencx - Accessed . https://www.scien.cx/2023/02/11/redux-like-state-container-in-swiftui-basics/
IEEE
" » Redux-like state container in SwiftUI. Basics.." Sergey Leschev | Sciencx [Online]. Available: https://www.scien.cx/2023/02/11/redux-like-state-container-in-swiftui-basics/. [Accessed: ]
rf:citation
» Redux-like state container in SwiftUI. Basics. | Sergey Leschev | Sciencx | https://www.scien.cx/2023/02/11/redux-like-state-container-in-swiftui-basics/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.