I have a file named Card.js. It contains two states called status
and view
.
Every time a button is clicked, my status
state changes & according to this state my card
is hidden.
At the same time, each of my cards
has a view
state. This increases by 1 according to the click event.
src/components/Card.js
const Card = (props) => {
const [status, setStatus] = useState(false);
const [view, setView] = useState(0);
const handleClick = () => {
setStatus(!status);
setView(view + 1);
};
return (
<div>
<Button onClick={handleClick}>{status ? "Show Component" : "Hide Component"}</Button>
{status ? null : <div className="card">{props.children} </div>}
</div>
);
};
export default Card;
In my app.js
file, I return the data in my JSON file with the map method, I print this data to my card
component.
src/App.js
const App= () => {
return (
<div>
{post.map((value, index) => {
return (
<Card key={index}>
{// here I want to print the "view" state in my Card.js file.}
</Card>
);
})}
</div>
);
};
export default App;
In App.js
, I tried to create the view
state using the useEffect
hook, but every time I click the button, the view
state of both my cards is updated at the same time. I don't want it to happen this way.