Skip to content

unixzii/SwiftUI-Hooks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SwiftUI Hooks

Note: This is only a proof of concept, not for production use yet.

Quick Start

let ContentView = { RendererView { hooks in
    let (counter, setCounter) = hooks.useState(initial: 0)
    hooks.useEffect {
        if counter == 5 {
            alert("High five!")
        }
    }
    hooks.useEffect({
        alert("Hi there!")
    }, dep: RendererView.triggerOnce)

    return VStack {
        Text("Current value: \(counter)")
        HStack {
            Button(action: { setCounter(counter + 1) }) { Text("+") }
            Button(action: { setCounter(counter - 1) }) { Text("-") }
        }
    }
    .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)>*
} }

Prior Art

This is fully inspired by React Hooks. To learn more about what hooks are doing, please first check out the React documentation. And the API design in this PoC also follows the React spec, you can literally map what you learned to this.

Examples

API References

TBD.

See RendererView.swift.

Todos

  • useEffect with cleanup.
  • Detection of inconsistent hook calls.
  • ...

Contribution

This repository won't accept PRs about detail design. If you are interested in making it a library, feel free to leave an issue and let me know.

About

A PoC for implementing hooks in SwiftUI

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages