Dynamic Props & Classes
Concisely update component props without updating the component
Why Dynamic Props & Classes
Previously in WebLabs, we updated a whole component in case we wanted to update anything about it, be it props or something else.
This means WebLabs would build the component from scratch even if you wish to update only a class or a property.
Dynamic Props & Classes builds the class value and property value when those state values are updated, instead of building the entire component that is expensive
Dynamic Prop
The core/components
now comes with a new method, .dynamicProp()
method. This function takes a state type ( for TS only ).
You provide the name of the property, a value builder function and a dependency to depend on the update. This uses the latest State Subscription from weblabs.
Let's see it in action!
Above, you can see a div with a dynamic property of id, with a value builder function that returns the value of the color set, and obvious state dependency is the state itself, i.e color.
We did not wrap our component in $()
function, but this will still update, but it will update only the id
of the div component.
This will improve the performance very much since all the other stuff will not be re-created is you wish to update something that is not data-related.
Dynamic Class
The core/components
now come with another new method .dynamicClass()
that takes a value builder function, and returns a single no-space separated character.
In this function, you provide a value builder function, which MUST return a single string that needs to be updated, and link it to a state that it might depend on.
Here is an example:
In the style.css file, there is the classes for red and blue. It will update only the class name specifically the one that you are linking to, without recreating the component from scratch.
DO NOT USE LIKE THIS
The dynamic class is made to handle only a singular string value with no spaces as of now.
DO THIS INSTEAD
This will keep the dynamic class apart from constant class names, it is easier to read as well, and it is very performant
If you have multiple dynamic classes that needs to be updated, use the .dynamicClass()
function multiple times, as it can only return a singular string that needs to be updated.
Last updated
Was this helpful?