I trying to do parent and child recursive state using React. I have added App3 which has initial state should maintain entire state into it. I have no idea how can I add update delete with this when it comes to recursive.
I have tried using https://hookstate.js.org/docs/recursive-state/ But due to some limitation I want to use react without any third party packages.
How can I perform CRUD for the recursive state
const data = {
op: 0,
values: [
{
op: 0,
value: {
values: [
{
op: 2,
value: {
values: []
}
}
]
}
},
{
op: 1,
value: {
values: []
}
}
]
}
function App3() {
const [mainState, setMainState] = React.useState(data);
return (
<div>
{
mainState.values.map((x, i) => {
return <RecursiveFn key={i} value={x}></RecursiveFn>
})
}
<button>Add</button>
<hr/>
<div><pre>
{JSON.stringify(mainState)}</pre></div>
</div>
)
}
function RecursiveFn(props) {
return (
<div style={{ padding:5,margin:10,paddingLeft: 20,border:'1px solid black' }}>
<div> OP -
<input type={'text'} value={props.value.op}></input>
</div>
{props.value.value && props.value.value.values ? props.value.value.values.map((x, i) => <RecursiveFn value={x}></RecursiveFn>) : null}
<button>Add</button>
<button>Delete</button>
</div>
);
}
ReactDOM.render(<App3 />,
document.getElementById("root"))
<script src="https://unpkg.com/react@16.7.0-alpha.0/umd/react.production.min.js">
</script>
<script src="https://unpkg.com/react-dom@16.7.0-alpha.0/umd/react-dom.production.min.js"></script>
<div id="root"></app>