Immutable Types
What are immutable types?
These are types that you can instantiate but never mutate the values. In order to update a value, you must instantiate a new value.
Why using immutable types?
Properties, like in React, are propagated from ancestors to
children. This means that the properties must live when each component is
updated. This is why properties should —ideally— be cheap to clone. To
achieve this we usually wrap things in Rc
.
Immutable types are a great fit for holding property's values because they can be cheaply cloned when passed from component to component.
Common Immutable Types
Yew recommends using the following immutable types from the implicit-clone
crate:
IString
(aliased asAttrValue
in Yew) - for strings instead ofString
IArray<T>
- for arrays/vectors instead ofVec<T>
IMap<K, V>
- for maps instead ofHashMap<K, V>
These types are either reference-counted (Rc
) or static references, making them very cheap to clone.