In my component, I have a child component (FooComponent) where I pass a parameter to it and also get a ref.
Parent.razor
<FooComponent @ref="FooComponentRef" FooParameter="@MyValue"/>
<button @onclick="Test">@MyValue</button>
@code {
FooComponent FooComponentRef { get; set; }
string MyValue { get; set; }
int count { get; set; }
void Test(){
MyValue = "SomeValue" + count++;
FooComponentRef.FooFunction();
}
}
I modified the code so it's a simple and testable example
And I have a function (Test) that will update the property that is passed in the parameter (MyValue passed to FooParameter) and call a function from the ref (FooFunction of FooComponent).
And inside the FooComponent
FooComponent.razor
<div>
<div>FooParameter: @FooParameter</div>
<div>ValueUsedInFooFunction: @ValueUsedInFooFunction</div>
</div>
@code {
[Parameter]
public string FooParameter { get; set; }
public string ValueUsedInFooFunction { get; set; }
public void FooFunction()
{
// This function is using FooParameter to make some logic
ValueUsedInFooFunction = FooParameter;
}
}
I modified the code so it's a simple and testable example
It uses the FooParameter in the FooFunction to do some logic.
The problem is that when I change the MyValue and call FooFunction, the component didn't update yet, so it uses the "old" value of FooParameter, but I need to use the new setted value, which is the correct MyValue.
Another thing that makes this hard to solve is that I can't change the FooFunction (it's not a function that I created).
I have tried using everything (StateHasChanged, InvokeAsync) but still no solution.
What I need is my Test function to do something like
void Test(){
MyValue = "SomeValue" + count++;
// Somehow update UI so FooParameter have the correct value of MyValue
FooComponentRef.FooFunction();
}
Here is the blazor fiddle to test
In the fiddle, when clicking in the button and calling Testfunction, you will see that the ValueUsedInFooFunction is always the "old" value of FooParameter, meaning that the FooFunction is being executed before the FooParameter is being updated.