Blazor - position component inside user content #27615
              
                Unanswered
              
          
                  
                    
                      dennis-yemelyanov
                    
                  
                
                  asked this question in
                Q&A
              
            Replies: 1 comment 2 replies
-
| 
         as you mentioned they suggest (and I think its a good idea) to not manipulate DOM elements directly while using Blazor.  | 
  
Beta Was this translation helpful? Give feedback.
                  
                    2 replies
                  
                
            
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
        
    
Uh oh!
There was an error while loading. Please reload this page.
-
I have a requirement to dynamically put a Blazor component inside user-provided content. Essentially, the component is supposed to extend user-provided markup with some UI elements.
Let's say the user provides some content that can have a "greeting-container" element in it and the component should insert a greeting button inside that element.
My current solution is to call a JavaScript function to move the DOM element in
OnAfterRenderAsync(full code below). It seems to work fine, but manipulating DOM elements seems to be discouraged in Blazor since it can affect the diffing algorithm. So I have a couple of questions on this:RenderTreeBuilderfor this, but it seems like it might not be designed for this purpose since it's recommended to use hardcoded sequence numbers, which doesn't seem possible when dealing with dynamic content not known at compilation time.Current solution code:
Greeter.razor
_Host.cshtml
UserContentTest.razor
Expected result (after clicking "Toggle greeting"):
Beta Was this translation helpful? Give feedback.
All reactions