I have the following array that include objects using React, this array display the users fields data in a specific page.
What is the right way to update this component using useEffect, specifically the array on load of the page before data are rendered, to include:
new element to push in the array
(on load of the page)
userData.splice(3, 0,
{
key: "gender",
label: "DFR",
},
);
Initial Array
const userData = [
{
key: "first_name",
label: "XYZ",
},
{
key: "last_name",
label: "XYYZ",
},
{
key: "username",
label: "ABC",
},
{
key: "age",
label: "ABCC",
},
{
key: "nationality",
label: "EDP",
},
];
I have tried the following but for some reason it wont work:
useEffect(() => {
userData.splice(3, 0,
{
key: "gender",
label: "DFR",
},
);
}), [];
P.S: As you can see, i don't intend to use push.
push?spliceinstead because i want the data to be displayed at a specific position, also i need that logic to be triggered on load of the page