Skip to content

Commit

Permalink
fix(ios): initialContext not always set before name (#17)
Browse files Browse the repository at this point in the history
* fix(ios): Application of props is not guaranteed to be in any specific
order. Setting initialContext may occur before the name prop is set and
therefore will be unable to set any initialContext. To address this, we now store
the initialContext on the view itself and then apply it to the portal
just before rendering.

* fix(ios): Set initialContext on portal if it is already present
  • Loading branch information
Steven0351 authored Jul 1, 2022
1 parent d45ad41 commit 5ebef45
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions ios/ReactNativePortals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class PortalViewManager: RCTViewManager {

class PortalView: UIView {
private var webView: PortalUIView?
private var _initialContext: [String: JSValue]?

@objc var name: String? {
get {
Expand All @@ -88,21 +89,22 @@ class PortalView: UIView {
}

@objc var initialContext: [String: Any]? {
get {
guard let portal = _portal else { return nil }
return portal.initialContext
}

get { _initialContext }
set {
guard let name = name else { return }
_portal = PortalManager.getPortal(named: name)
_portal?.initialContext = JSTypes.coerceDictionaryToJSObject(newValue) ?? [:]
_initialContext = JSTypes.coerceDictionaryToJSObject(newValue)
// If the Portal is already present then we need to set the initialContext so it's the `didSet` property observer is fired
_portal?.initialContext = _initialContext ?? [:]
}
}

private var _portal: Portal? {
didSet {
guard let portal = _portal else { return }
guard var portal = _portal else { return }

if let initialContext = _initialContext {
portal.initialContext = initialContext
}

DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
self.webView?.removeFromSuperview()
Expand Down

0 comments on commit 5ebef45

Please sign in to comment.