Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

182
Visualizações
are there anyway to prevent the OnAfterRenderAsync to run the JS function for every rendering in Blazor?

I call jquery fun in my blazor app. I'm doing some services in Onin - I would like to call this function in my razor component. I did use the following code in the component. The function is showing when the page has loaded. But when I doing any clicking or changing any value in the page, even like a change a text that not related to the function, the function existed every time. Any hint?

 protected override  void OnAfterRender(bool firstRender)
    {
        
            
              if(!firstRender)
    {
           await JsRuntime.InvokeVoidAsync("TextArea.AddLineToTextArea");
           
     }   

You may wonder why I put (!firstRender), the reason is that by default the firstRender is false, and I don't know the reason. Some people say ,According to this Blazor Component Reference Null on First Render, that if your component in if/else it will cause the issue. but mine is out side

here is my function is adding a linenumber to textarea.

window.TextArea = {
    
    AddLineToTextArea:function(){

        $(".lined").linedtextarea(
            {selectedLine: 1}
          );
        
          // Target a single one
          $("#inpDefinition").linedtextarea();
    }

}



about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you reverse the if condition you should have what you need:

protected override void OnAfterRender(bool firstRender)
    {
        if (firstRender)
        {
            await JsRuntime.InvokeVoidAsync("TextArea.AddLineToTextArea");
        }
    }

This code means that the await JsRuntime.InvokeVoidAsync("TextArea.AddLineToTextArea"); statement will be executed only once, when the component has it first rendering.

The method OnAfterRender is executed everytime the component is updated, for example a user click on a button or whatever. But the boolean parameter firstRender is equal to true for the first rendering, and will be false after.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda