So I am new to working on Electron and React. I wanted to implement more functionality but I have run into an issue where when I pass function c into a new Card element it seems to pass the hook in function c at the time of creation.
Such that: When the + button is clicked 3 times it would render 3 Cards as expected, lets call them 0, 1 and 2. However, when their log button is clicked to console log the cards hook, clicking 0's log button console logs:
{}
clicking 1's log button console logs:
{0: {react element of 0}}
clicking 2's log button console logs:
{0: {react element of 0}, 1: {react element of 1}}
However, clicking the - button which i have rigged to troubleshoot this at the moment in the App.js produces:
{0: {react element of 0}, 1: {react element of 1}, 2: {react element of 2}}
Am I wrong to assume that clicking each log button should have instead console logged what the - button in the App.js console logged. Rather than what the hook looked like at the time of the Card elements creation? How would I go about fixing this issue?
Note: the reason why I use an Object to store the React elements instead of an Array is because I would like to eventually add a remove button to remove individual cards. Is there a better way to do what I am doing?
App.js
import './App.css';
import Cards from './Cards/Cards'
import { useState } from 'react';
let KEY = 0
export default function App() {
const [cards, setCards] = useState({})
const c = () => {
console.log(cards)
}
const make = () =>{
const a = <Cards log={c} anID={KEY}/>
const b = {...cards}
b[KEY] = a
KEY += 1
setCards(b)
}
return (
<div>
<div className="time-container">
{Object.values(cards)}
</div>
<button className="add-button" onClick={() => make()} >+</button>
<button className="" onClick={() => console.log(cards)} >-</button>
</div>
);
Card.js
import React from 'react'
import './Cards.css'
export default function Cards({ log, anID }) {
return <div className="card-container">
<div>
<button className="card-buttons-red" onClick={()=> log(anID)}>Log</button>
<button className="card-buttons">Edit</button>
</div>
</div>
}