Question for Beginners - I am using SolidJS and I need help making DOM elements react to signal updates.
I am experiencing issues with two instances that are not updating as expected:
- In the Main Component, when trying to update the item count.
- In the Secondary Component, when trying to update the item results.
Main Component
import { Component } from 'solid-js'
import { isEqual as _isEqual } from 'lodash-es'
const [getItems, setItems] = createSignal<Array<Item>>([])
const MainComponent: Component = () => {
const updateItems = (item: Item) => {
const initialItems = getItems()
const index = initialItems.findIndex(i => _isEqual(i, item))
index == -1 ? initialItems.push(item) : initialItems.splice(index, 1)
setItems(initialItems)
}
return (
<>
<span>Total Items: {getItems().length}</span> // << -- WON'T UPDATE.
<button onclick={() => updateItems(x: Item)}>Click Me</button>
</>
)
}
export { MainComponent, getItems }
Secondary Component
import { Component } from 'solid-js'
import { getItems } from '../MainComponent'
const SecondaryComponent: Component = () => {
return (
<p class='results'>
{getItems() ? 'Contains Items' : 'No Items'} // << -- WILL NOT CHANGE.
</p>
)
}