-
I'm testing out the import { useStore } from '@nanostores/react'
import { deepMap } from 'nanostores'
const $areas = deepMap({})
$areas.setKey('A1', { x: 0, y: 0, width: 10, height: 10 })
$areas.setKey('A2', { x: 10, y: 0, width: 10, height: 10 })
const AreaWidth = ({ id = 'A1' }) => {
// Need $areas[id].width here, and re-render when $areas[id].width changes only
const a1WidthOnly = useStore($areas)
return <div>{a1WidthOnly}</div>
} Can anyone provide any example? |
Beta Was this translation helpful? Give feedback.
Answered by
ai
Mar 22, 2024
Replies: 1 comment 1 reply
-
For non-dymamic path you can use $a1WidthOnly = computed($areas, areas => areas.a1.width)
const Area1Width = ({ id = 'A1' }) => {
// Need $areas[id].width here, and re-render when $areas[id].width changes only
const a1WidthOnly = useStore($a1WidthOnly) For you example with area ID from props, I would create a store for each area: let areas = {}
areas.a1 = map({ x: 0, y: 0, width: 10, height: 10 })
areas.a2 = map({ x: 0, y: 0, width: 10, height: 10 })
const AreaWidth = ({ id = 'A1' }) => {
// Need $areas[id].width here, and re-render when $areas[id].width changes only
const a1WidthOnly = useStore(areas[id], { keys: 'width' }) Or you can create let cache = {}
function getComputed(id) {
if (!cache[id]) {
cache[id] = computed($areas, areas => areas[id].width)
}
return cache[id]
}
const Area1Width = ({ id = 'A1' }) => {
// Need $areas[id].width here, and re-render when $areas[id].width changes only
const a1WidthOnly = useStore(getComputed(id)) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
jozefini
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For non-dymamic path you can use
computed()
:For you example with area ID from props, I would create a store for each area:
Or you can create
computed
store on the …