I have an array of objects which I want to map it and set the objects into the messagesList state, but only the last object is set. Actually, I want the setting to be done as soon as the map is executed but here it is done only once and the last item of the array is set.
You can see the codes:
const steps = [
{
id: "ab4acc83-f732-443a-9c05-f6fee37f9116",
order: 1,
schema: {
answer: "answer 1",
process: "display",
outputType: "text",
},
FlowId: "c0967d44-a218-443e-afd5-5f113ad4d48e",
createdAt: "2022-10-23T05:35:05.235Z",
updatedAt: "2022-10-23T05:35:05.235Z",
},
{
id: "ab4acc83-f732-443a-9c05-f6fee37f9116",
order: 1,
schema: {
answer: "answer 2",
process: "display",
outputType: "text",
},
FlowId: "c0967d44-a218-443e-afd5-5f113ad4d48e",
createdAt: "2022-10-23T05:35:05.235Z",
updatedAt: "2022-10-23T05:35:05.235Z",
},
];
steps.map((step) => {
setMessagesList([
...messagesList,
{
dataType: "text",
item: {
id: uuidv4(),
type: "sender",
message: "Question",
},
},
{
dataType: "text",
item: {
id: uuidv4(),
type: "reciever",
message: step.schema.answer,
},
},
]);
});
Now MessagesList List only consists the last item of the array