Announcing Atoms - A new state management library

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.

3 Likes

Do the atoms have to be global? What if you want to have multiple instances of a view with different data?

No the atoms does not have to be global. The todo example demonstrates how you can create new atoms on the fly. The readme could probably do a better job explaining this.

It is also perfectly fine to have a child view that accepts a binding to some element inside an array atom. Similar to how @State and @Published works.