Hello, I have a question about creating objects and passing them as reference down to children components as props.
I have the following code pieces:
const playerStatController = new PlayerStatController(playerStats,(value)=>{
console.log("Should update DOM: ",value);
setPlayerStats(value)
});// Instances a class that controls player stats state
const cardGenerator = new CardGenerator(playerStatController);// Instances a class that creates cards
const [currentDeck,setCurrentDeck] = useState<Card[]>(cardGenerator.GenerateCards(INITIAL_DECK)); // Using the card generator object, Generate Cards method will create objs based on passed ID's and inject them with the stat controller
The idea is that I have an object responsible for handling the state, which works if I directly access its methods with a test function, as the DOM updates with expected values.
Every "card" object has access to the stat handling object, as a reference is passed upon instancing.
They all could access the stat handling object and perform DOM updates, however there would be weird interactions with multiple cards trying to update the DOM at the same time with different values... therefore I made an accumulator method inside the stat handling object, that will simply go through all the cards and add their values, then dump the result in the stat.
And that's where things started happening weirdly.