I am trying to insert the first value in a array in a useState but it comes as undefined. I know that the array contains the value that.
let {id} = useParams();
const [currentJacuzzi, setCurrentJacuzzi] = useState();
const {jacuzzis} = useContext(JacuzziContext);
const [mappedJacuzzis, setMappedJacuzzis] = useState([]);
useEffect(() => {
const tempArr = jacuzzis.filter(jacuzzi => jacuzzi.brand === id);
const mapped = tempArr.map((obj) => (
{
name: obj.name,
about: obj.aboutProduct,
image: obj.images[0].image
}
));
setMappedJacuzzis(mapped);
setCurrentJacuzzi(mappedJacuzzis[0]); //-- mappedJacuzzis contains an array with objects at this point. But
}, [jacuzzis]);
mappedJacuzzis contains a array with 3 objects, I wish to get a new value containing only the first object in this array. currentJacuzzi should contain the first value, but is undefined.
<Row className="justify-content-center">
<h1>{currentJacuzzi.name}</h1>
</Row>
<Row className="justify-content-center mr-5 ml-5">
<p>{currentJacuzzi.about}</p>
</Row>