Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
beyaz committed Feb 3, 2024
1 parent a363af7 commit bcfa776
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
10 changes: 2 additions & 8 deletions ReactWithDotNet/ElementSerializer.ToJsonMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,15 +426,9 @@ public static async Task<IReadOnlyJsonMap> ToJsonMap(this Element element, Eleme
if (node.FunctionalComponent.Constructor is not null)
{
await node.FunctionalComponent.Constructor.Invoke();

// recalculate scope because maybe fields have changed
node.FunctionalComponent.Scope = SerializationHelperForCompilerGeneratedClasss.Serialize(node.FunctionalComponent.Constructor.Target);
}
else
{
// recalculate scope because maybe fields have changed
node.FunctionalComponent.Scope = SerializationHelperForCompilerGeneratedClasss.Serialize(node.FunctionalComponent._target);
}

node.FunctionalComponent.CalculateScopeFromTarget();
}


Expand Down
25 changes: 25 additions & 0 deletions ReactWithDotNet/FC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,31 @@ sealed class FunctionalComponent : Component, Scope
/// </summary>
public IReadOnlyDictionary<string, object> Scope { get; set; }

internal void CalculateScopeFromTarget()
{
object target;

if (renderFuncWithScope is not null)
{
target = renderFuncWithScope.Target;
}
else if (renderFuncAsyncWithScope is not null)
{
target = renderFuncAsyncWithScope.Target;
}
else
{
target = _target;
}

if (target is null)
{
throw DeveloperException("Invalid usage of useState. target not calculated.");
}

Scope = SerializationHelperForCompilerGeneratedClasss.Serialize(target);
}

public void InitializeTarget()
{
_target ??= SerializationHelperForCompilerGeneratedClasss.Deserialize(CompilerGeneratedType, Scope);
Expand Down

0 comments on commit bcfa776

Please sign in to comment.