Skip to content

How to select nested property using deep-map in react #282

Answered by ai
jozefini asked this question in Q&A
Discussion options

You must be logged in to vote

For non-dymamic path you can use computed():

$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 computed store on the …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@jozefini
Comment options

Answer selected by jozefini
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants