As we know component/element references are getting filled when component is rendered, we
use OnAfterRender to do some actions with referenced component/element.
OnAfterRender gets executed each time component re-renders and in most cases we don't need to do some action with referenced component/element each time.
To avoid this we are forced to do it only first time OnAfterRender is called.
example
public CompBlazorScrollbar CompBlazorScrollbar1;
bool FirstLoad = true;
protected override void OnAfterRender()
{
if (FirstLoad)
{
FirstLoad = false;
CompBlazorScrollbar1.OnPositionChange += OnPositionChanged;
base.OnAfterRender();
}
}
This technique solves problem but this method gets executed each time on re-render and extra work is made to check FirstLoad value also having this extra variable in code.
If it is possible, would be great to have separated lifecycle method which will execute once as OnInit but with filled component/element references.
It is not feature we can't live without but I think will be helpful and convenient to have it.
Thanks for contacting us, @Lupusa87.
@SteveSandersonMS thoughts? I agree that a separate lifecycle event (maybe named OnAfterFirstRender) would be useful.
Yes, either OnAfterFirstRender or OnAfterRender(bool isFirstRender) seem like they would be useful.
I'm gonna let @SteveSandersonMS chip in here, but based on previous discussions for similar things (JSInterop) we want to limit the amount of lifecycle methods that we include. As you mention it is already possible to achieve this and we are not sure about how common this scenario is for application developers (vs for example library authors), so we want to be conservative here.
We can always add lifecycle methods later, but taking them away is harder.
@SteveSandersonMS I'm wondering:
Is it possible to change a razor component base class from the .razor file?
This way you could create your own custom lifecycle methods based on ours by implementing the above pattern in a base class (that extends ComponentBase). and simply use that across your components
@javiercn thank you,
I understand what you say and it makes lot of sense.
Sure it is possible developer to check if OnAfterRender is first time but he/she first needs to realize that this should be under control.
OnAfterRender is calling each time when component re-renders and any code inside will be called each time too, if there is any code causing re-render we will have infinite loop, this is not big issue because developer can see problem and guess why this happens.
If there is code which not re-renders component developer can not guess that this code is executing unnecessary each time and it can cause unexpected behaviors.
If we decline this update it will not be big issue, just docs should cover possible problems developers to realize and avoid them.
Yes, either
OnAfterFirstRenderorOnAfterRender(bool isFirstRender)seem like they would be useful.
If we do not want to add new lifecycle method then we can do @SteveSandersonMS suggested second option.
We don鈥檛 want to add new lifecycle methods, the recommended way to do this is to add it to OnAfterRender and use a Boolean flag to prevent executing it multiple times.
We expect this not to be very common unless you are implementing a component library, in which case you can create a base class library for your components extending component base and do it yourself there.
Most helpful comment
Yes, either
OnAfterFirstRenderorOnAfterRender(bool isFirstRender)seem like they would be useful.