https://github.com/unixzii/swiftui-hooks
A PoC for implementing hooks in SwiftUI
https://github.com/unixzii/swiftui-hooks
apple hooks ios react swift swiftui
Last synced: about 1 month ago
JSON representation
A PoC for implementing hooks in SwiftUI
- Host: GitHub
- URL: https://github.com/unixzii/swiftui-hooks
- Owner: unixzii
- Created: 2019-06-12T05:28:09.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-06-12T13:55:16.000Z (almost 6 years ago)
- Last Synced: 2025-03-20T13:13:31.136Z (about 1 month ago)
- Topics: apple, hooks, ios, react, swift, swiftui
- Language: Swift
- Size: 22.5 KB
- Stars: 82
- Watchers: 2
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# SwiftUI Hooks
> Note: This is only a proof of concept, not for production use yet.
## Quick Start
```swift
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](https://reactjs.org/docs/hooks-intro.html). 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
* [Todo List](https://github.com/unixzii/SwiftUI-Hooks/blob/master/SwiftUI-Hooks/ContentView.swift#L59)
## API References
*TBD.*
See [RendererView.swift](https://github.com/unixzii/SwiftUI-Hooks/blob/master/SwiftUI-Hooks/RendererView.swift#L12).
## 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.