I am trying to implement a property (real estate) chain
Each chain item should know which chain item is above/below it
A chain item can have its own sub-chain, and this in turn applies to its sub-chain items as well and so on
Visually like this
[
{
id: 1,
belowId:0
},
{
id: 0,
belowId:2,
aboveId:1,
subChain: [{ id:3, subChain: [{ id:4}] }]
},
{
id: 2,
aboveId:0
}
]
I tried this above structure but I started getting confused how to update nested items in state and ui
2
Answers
As no one has answered my question, maybe I wasn't clear enough too to be fair. I will answer it here so that if someone stumbles on a similar problem.
So the main idea is that you want to flatten the array and make sure that
subChain
only consist ofids
not references.Here is how the
ChainItem
look like (state structure)I create an item with this utility method
How it looks in the UI
I build the UI tree like this
React
stateCreate a sibling
Create a sub chain
You should look into a state management library like Redux. With Redux you’ll have a global state store that all of your components can pull from.