Component Children, Keys and Fragments
Children
Components often have child components. If you consider HTML tags like DIV
, UL
, and TABLE
you will see you are already familiar with this concept:
Here we have a DIV
that receives one param, an id equal to 1 and has two child elements - the two spans.
The SPAN
s each have one param (its css class) and has one child element - a string to render.
Hopefully at this point the DSL is intuitive to read, and you can see that this will generate the following HTML:
Dynamic Children
Children do not have to be statically generated. Let's sort a string of text into individual word counts and display it in a list:
In this case you can see that we don't determine the actual number or contents of the LI
children until runtime.
Dynamically generating components creates a new concept called ownership. More here...
Keys
In the above example what would happen if the contents of text
were dynamically changing? For example if it was associated with a text box that the user was typing into, and we updated text
whenever a word was entered.
In this case as the user typed new words, the word_count
would be updated and the list would change. However actually only the contents of one of the list items (LI
blocks) would actually change, and perhaps the sort order. We don't need to redraw the whole list, just the one list item that changed, and then perhaps shuffle two of the items. This is going to be much faster than redrawing the whole list.
Like React, Hyperstack provides a special key
param that can identify child elements so that the rendering engine will know that while the content and order may change on some children, it can easily identify the ones that are the same:
You don't have to stress out too much about keys, its easy to add them later. Just keep the concept in mind when you are generating long lists, tables, and divs with many children.
Rendering Children
Application defined components can also receive and render children. A component's children
method returns an enumerable that is used to access the unrendered children of a component. The children can then be rendered using the render
method which will merge any additional parameters and render the child.
Rendering Multiple Values and the FRAGMENT component
A render block may generate multiple values. React assumes when a Component generates multiple items, the item order and quantity may change over time and so will give a warning unless each element has a key:
If you are sure that the order and number of elements will not change over time you may wrap the items in the FRAGMENT
pseudo component:
Last updated