I'm building a React app and trying to see if I can do the following. there are a lot of nested component and the one being updated is always the top parent.
The idea is to build it through nested object prototyped via ES6 class like this:
class _Board {
constructor(setStateFunction){
this.items = [];
this.setState = setStateFunction;
}
addSwimlane() {
console.log(this);
this.items = [...this.items, new _Swimlane()];
this.setState(this);
}
}
class _Swimlane {
constructor(){
this.id = uuidv4();
this.items = [];
}
addColumn() {
this.items = [...this.items, new _Column(this.id)];
//Can we call the parent function through super to update the render?
}
}
I then have my component in which I would call the function:
const Main = (props) => {
const [board, setBoard] = useState(new _Board());
useEffect(() => { //Initial Render load setState
setBoard(new _Board(setBoard));
}, []);
return (
<div className="App" >
{JSON.stringify(board)}
<button onClick={() => board.addSwimlane()} >Add Swimlane</button>
</div>
);
};
I have two question:
- How do you pass function like this in a class? I am able to pass them through arrow function like normal but when passed as parameter, the render doesn't update.
- How do you call the parent class once the children has been nested? Should I pass the function through the "addX" function I already have and it will be there as reference?