I am trying to add an element into an array after I click the "click me" button. I did a console log to make sure it went inside the function, but despite the function being call, nothing changes.
I also try using useEffect to check if my variable got update
Relevant code:
import React, { useState, useEffect } from "react";
export default function App() {
let [test, setTest] = useState(["1", "2", "3"]);
const onChange = () => {
console.log("onChange");
test.unshift("4");
};
const cards = () => {
return (
<div>
{test.map((item, i) => (
<ul key={i}>
<li>{item}</li>
</ul>
))}
</div>
);
};
useEffect(() => {
console.log("cardData ", test);
}, [test]);
return (
<div className="App">
<button onClick={() => onChange()}>Click me</button>
{cards()}
</div>
);
}
Really need some advice on where did i go wrong
This is my codesandbox link