i created empty array using the React State Hook, the array starts empty and i have 3 functions to (add to the array), (remove the last element of the array) and another to empty the array. The functions seems to work since i logged the result in the console. But when i want to print the array on a paragraph, the array does not show. Can someone give me some feedback? Thanks.
The code is below
import './counter.css';
import { useState } from 'react';
export default function HandleArray() {
const [inicial, handler] = useState([]);
function addElement(element) {
inicial.push('ok');
handler(inicial);
console.log(inicial);
}
function removeElement() {
inicial.pop();
handler(inicial);
console.log(inicial);
}
function reset() {
inicial.length = 0;
handler(inicial);
console.log(inicial);
}
return (
<div className='card text-bg-light border-success mb-3'>
<div className='card-header'>Welcome</div>
<div className='card-body'>
<h5 className='card-title'>Manipulate the array</h5>
<button type='button' className='btn btn-dark ' onClick={addElement}>
Insert Element
</button>
<button
type='button'
className='btn btn-danger'
onClick={removeElement}
>
Remove Element
</button>
<button type='button' className='btn btn-warning' onClick={reset}>
Reset array
</button>
//log the array
<p className='card-text'>{inicial}</p>
</div>
</div>
);
}