Hello everyone!
I’m really excited to share some new open source I’ve been working for a while.
Atoms is a state management library created specifically for use with SwiftUI. Its inspiration comes from Jotai, a popular React framework. Its goal is to simplify state management by forming small, modular units that can be combined throughout your application as one source of truth, making it easier to use only what is essential in terms of state management, without having to worry about the complexities of object-oriented programming.
// Create a text atom
let textAtom = Atom("")
// Create a derived atom that depends on textAtom.
// Atoms automatically update their state when any of their dependencies change.
let extractedNumbersAtom = DerivedAtom {
@UseAtomValue(textAtom) var text
return text.filter {
$0.isNumber
}
}
struct ContentView: View {
// Provide write access to the textAtom
@UseAtom(textAtom) var text
// Provide read-only access to the extractedNumbersAtom
@UseAtomValue(extractedNumbersAtom) var numbers
var body: some View {
VStack {
TextField("", text: $text)
Text("Extracted numbers: \(numbers)")
}
}
}
I always found the topic of state management fascinating, and I hope some of you will find it useful. Repo is available here.