I have a React project where in the useEffect hook there is an eventListener with which the data of the event will be displayed. At the first attempt it works well but when another event is coming and I changed the state of the array which contains the elements which need to be displayed the component doesn't rerender.
App.js
import React, { useEffect, useReducer, useState } from "react";
function App(){
const [dataTableElements, setDataTableElements] = useState([]);
const sendMessage = (message) => {
window.opener.postMessage(message, '*');
}
const receiveMessage = (e) => {
if (e.data.source === 'source') {
if (e.data.type === 'SEND_DATA') {
const message = e.data.message;
createElements(message);
}
} else {
return;
};
};
const createElements = (data) => {
var temp = Object.assign([], dataTableElements);
temp = [];
{Object.keys(data).forEach((value, index) => {
temp.push(<MyComponent key={value + " " + index} data={data[value]} />)
})}
setDataTableElements(temp);
}
useEffect(() => {
sendMessage(message_READY);
window.addEventListener("message", receiveMessage, false);
return () => window.removeEventListener("message", receiveMessage);
}, []);
return (
<Layout className="mainLayout">
<Header>
<AppHeader />
</Header>
<Content>
<div className="tablesContainer">
{dataTableElements}
</div>
</Content>
</Layout>
);
}
export default PopupWindow;