MDC 2022 Practical Blazor.pptx

Jun. 22, 2022
MDC 2022 Practical Blazor.pptx
MDC 2022 Practical Blazor.pptx
MDC 2022 Practical Blazor.pptx
MDC 2022 Practical Blazor.pptx
MDC 2022 Practical Blazor.pptx
MDC 2022 Practical Blazor.pptx
MDC 2022 Practical Blazor.pptx
MDC 2022 Practical Blazor.pptx
MDC 2022 Practical Blazor.pptx
MDC 2022 Practical Blazor.pptx
MDC 2022 Practical Blazor.pptx
MDC 2022 Practical Blazor.pptx
MDC 2022 Practical Blazor.pptx
1 of 13

More Related Content

Similar to MDC 2022 Practical Blazor.pptx

AEM Sightly Deep DiveAEM Sightly Deep Dive
AEM Sightly Deep DiveGabriel Walt
Toms introtospring mvcToms introtospring mvc
Toms introtospring mvcGuo Albert
Asp.NET MVCAsp.NET MVC
Asp.NET MVCvrluckyin
Building richwebapplicationsusingaspBuilding richwebapplicationsusingasp
Building richwebapplicationsusingaspGiovanni Javier Jimenez Cadena
JavaScript Framework SmackdownJavaScript Framework Smackdown
JavaScript Framework Smackdownmeghantaylor
From Backbone to Ember and Back(bone) AgainFrom Backbone to Ember and Back(bone) Again
From Backbone to Ember and Back(bone) Againjonknapp

Recently uploaded

RPA for Finance and AccountingRPA for Finance and Accounting
RPA for Finance and Accountingessindiaseo
The art of AI ArtThe art of AI Art
The art of AI ArtDennis Vroegop
Sequence: Pipeline modelling in PharoSequence: Pipeline modelling in Pharo
Sequence: Pipeline modelling in PharoESUG
OutSystems Security Specialization - Study Help DeckOutSystems Security Specialization - Study Help Deck
OutSystems Security Specialization - Study Help DeckFábio Godinho
Top Benefits of Web based Systems. Top Benefits of Web based Systems.
Top Benefits of Web based Systems. sarasiva4
Jens Happe - Write tests you love, not hate.pdfJens Happe - Write tests you love, not hate.pdf
Jens Happe - Write tests you love, not hate.pdfJens Happe

MDC 2022 Practical Blazor.pptx

Editor's Notes

  1. 7
  2. When I first started working in component-based frameworks like Vue.js and Blazor, my instinct was often to make loops and then call a method from a binding inside of the loop.  After all, that's what we do in jQuery or even in everyday procedural programming, such as in a service or library function--when we have a loop, you use the properties of the objects we're looping over to make whatever call we want. But in web components, this logic can become much simpler by being eager to make a new subcomponent, moreso than your intuition might tell you. Usually breaking out the innards of a loop to its own component results in simpler code. Here's how you do it: In the original component, look at your loop; usually you will aim to refactor the entire contents of the loop (but not the loop flow control) into a new component. Choose a name for your component and insert markup for such an element (that doesn't yet exist), usually as the very first element inside the loop. It needs at least one parameter: the loop iteration reference. I like to name the parameter exactly the same as the loop iteration reference, as it makes the transition easier, which means it might be in camelCase and not PascalCase; this is temporary Copy the entire .razor component to a new .razor file that will become the child component. Rename the file to match the name you chose in step 1. If the component has a @page directive at the top of the new file, delete it. Rename that file to match the name you typed for the new element. Visual Studio unhelpfully edits your .csproj file to add a Watch-Remove element for the new component. I usually want to dotnet-watch all my components, so remove it from the .csproj file manually. Edit the new child component file markup to delete all content outside of and including the loop statement, leaving only the markup that was inside the loop. Add or modify the root-level element if desired. (I try always to have a root element with a class the same name as the component but in kebab-case.) Add a parameter member for the parameter you implied in step 1. It should be a public property with a getter and setter, and have a default value. Prefer it to be non-nullable if possible. Again, it might be in camelCase and not PascalCase; this is temporary. In the parent component, Visual Studio may give you the mistaken error "found markup with unexpected name", even though you are in the same folder and therefore the same namespace. Ignore it; it should compile anyway. This error might go away later when you reload Visual Studio. Look through the markup and see if there are any other missing references. There likely won't be, as you haven't yet deleted anything from the code section. If there are problems, fix them before continuing. At this point, your new markup in the parent component should work, and you should have a component that will compile and render the very same way, right above the old markup. Since you still have the same markup in the parent component, you can run it and see if you get the desired duplicated markup on the rendered page. If you do not, fix things before continuing. Put the new component and its parent on a diet Delete all the parts of the @code in the child component you no longer need; this is usually most of it. Also, delete any unneeded @inject directives and any unused @usings (for needs that were only in the parent and not here). Recompile and inspect it again, confirming that the new component is still identical to the old markup which still exists in the parent component. Go back to the parent component and delete the markup that is now being rendered in the child component. Also delete any code, @inject directives, or unused @usings that you can (for needs that were only in the child and not here). Recompile and inspect it again, confirming that the new component works well and that the old markup is gone. Fix its flaws Now that your component is fully baked, you can refactor it to make it simpler. Any bindings with arguments can likely now be argumentless, and the state for this iteration now simply becomes the state for the whole component instance. Finally, you can rename your new component's parameter to a better name with proper casing.   Done!