#How to update array in app.tsx from a component.tsx?

3 messages · Page 1 of 1 (latest)

zealous knot
#

In app.tsx at initiation contain an array that have objects like
{
id: 1,
Position: {x: 0, y:0},
data: {expanded: false}
}, …

And in component.tsx contains a button that when hit will toggle data.expanded = !data.expanded

How to I get it to update the array in app.tsx?

feral solar
#

@zealous knot You'd have to put the array in state and expose some API to the child for updating it, something like:

const toggleExpanded = (id: number) => {
    setValues(values.map(value => {
        return value.id === id ? {
            ...value,
            expanded: !value.expanded
        } : value
    }))
};
zealous knot
#

Will it be easier if I share the two codes I’m talking about? I am still not too sure about how to get it implemented.