It’s pretty easy to pass values from sibling components. We can do it in two steps:
Emit an event to the parent component
Pass the value as props down into the sibling component
For example, if we want to increase the child count value in Total Count component, we can emit the child count into the parent component. In this case, let’s emit the child count via a child-count event.
We should also do the same steps while parsing the attribute string for props. But since both tiny-listener and tiny-props use the same parser, we can create a function dedicated to parsing attribute strings.
export default function Tiny (options) {
// ...
function _addEventListeners (options) {
// ...
for (const listenerElement of listenerElements) {
const attribute = listenerElement.getAttribute('tiny-listener')
const values = _parseAttributeStrings(attribute)
// ...
}
}
// ...
function _addProps (comp) {
// ...
const props = _parseAttributeStrings(attribute)
// ...
}
// ...
}
Improving the Regex
We can refactor the three replace methods into one by adding an OR check in the regular expression. Characters that are checked with OR are wrapped within square brackets.
So the regular expression can become like this:
export default function Tiny (options) {
// ...
function _parseAttributeStrings (string) {
return string.trim()
.replace(/[[\],]/g, '')
.split(/\s+/)
}
}
Even though this regular expression looks complicated, you already know what it does.