index.js
const [todo, setToDo] = useState([]);
useEffect(async() => {
socket.on("receive_message", (message) => {
let result = todos ? todos : [];
if (message.sendBy === 'user') {
let old_index;
if (result.length > 0) {
old_index = result.findIndex(item => item.id === message.senderId);
if (0 >= result.length) {
var k = 0 - result.length + 1;
while (k--) {
result.push(undefined);
}
}
result.splice(0, 0, result.splice(old_index, 1)[0]);
setTodos(result); // for testing
}]
}); }, [todos]);
const render =
todos &&
todos.map((item) => {
return (
<li
key={item.id}
className={activeId.id === item.id ? "person active-user" : "person"}
data-chat="person1"
onClick={(e) => historyAccess(item)}
>
<div className="user">
<img
src={item.profile_pic ? item.profile_pic : "avatars/12.png"}
alt="Profile Picture"
/>
</div>
<p className="name-time">
<span className="name">{item.name ? item.name : ""}</span>
{/* <span className="time">{}</span> */}
</p>
</li>
);
});
useEffect is working fine and settodos is changing its value as array of objects but it does not re render. It is working absolutely fine and setTodos is changing it's value and working as expected then Why this is happening