Passing events from SwiftUI Button created by RN and getting it back to SwiftUI
This commit is contained in:
parent
41081b5084
commit
1a9884e0e9
6 changed files with 24 additions and 14 deletions
|
@ -8,9 +8,11 @@
|
|||
import SwiftUI
|
||||
|
||||
struct BaseButton: View {
|
||||
@EnvironmentObject var sharedState: SharedState
|
||||
|
||||
var body: some View {
|
||||
Button("SwiftUI Button") {
|
||||
|
||||
sharedState.text1 = "Clicked in SwiftUI button that was created in RN"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,7 +12,9 @@ import SwiftUI
|
|||
@objc(BaseButtonManager)
|
||||
class BaseButtonManager: RCTViewManager {
|
||||
override func view() -> UIView! {
|
||||
return UIHostingController(rootView: BaseButton()).view
|
||||
return UIHostingController(
|
||||
rootView: BaseButton()
|
||||
).view
|
||||
}
|
||||
|
||||
override static func requiresMainQueueSetup() -> Bool {
|
||||
|
|
|
@ -7,8 +7,6 @@
|
|||
|
||||
import Combine
|
||||
import React
|
||||
//import React_RCTAppDelegate
|
||||
//import ReactAppDependencyProvider
|
||||
|
||||
@objc(Emitter)
|
||||
class Emitter: RCTEventEmitter {
|
||||
|
@ -19,7 +17,6 @@ class Emitter: RCTEventEmitter {
|
|||
override init() {
|
||||
super.init()
|
||||
EventEmitter.sharedInstance.register(eventEmitter: self)
|
||||
|
||||
}
|
||||
|
||||
override func supportedEvents() -> [String]! {
|
||||
|
|
|
@ -15,6 +15,9 @@ final class SharedState: ObservableObject {
|
|||
var reactNativeFactory: RCTReactNativeFactory?
|
||||
private let emitter = EventEmitter.sharedInstance
|
||||
|
||||
// SwiftUI => RN => SwiftUI
|
||||
@Published var text1: String = ""
|
||||
|
||||
func send(message: String) {
|
||||
emitter.send(message: message)
|
||||
}
|
||||
|
|
|
@ -11,12 +11,14 @@ struct ToolboxHeader: View {
|
|||
@EnvironmentObject var sharedState: SharedState
|
||||
|
||||
var body: some View {
|
||||
HStack {
|
||||
Text("Actions")
|
||||
Button("Make it blue") {
|
||||
print("Making it blue")
|
||||
sharedState.send(message: "hello from Swift!")
|
||||
VStack {
|
||||
HStack {
|
||||
Text("Actions")
|
||||
Button("Make it blue") {
|
||||
sharedState.send(message: "hello from Swift!")
|
||||
}
|
||||
}
|
||||
Text("SwiftUI => RN => SwiftUI: \(sharedState.text1)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue