frank
07/30/2020, 10:47 AMval (mensajes, setMensajes) = useState(emptyArray<Json>()) //{ nick, msg, type}
val (users, setUsers) = useState(emptyArray<Json>())
useEffect(listOf(users)) {
socket.on("loginUser") { data ->
console.log("OnloginUser")
val noOfUsers = data["numOfUsers"] as Int
val item = getMsgJson("", getParticipantsMessage(noOfUsers), "log")
val items = mensajes.plus(item)
setMensajes(items) //render 1 time
setUsers(data["usersList"]) //render 2 time
}
}
christophsturm
07/30/2020, 10:49 AMfrank
07/30/2020, 10:54 AMAkif Abasov [JB]
07/30/2020, 1:09 PMfrank
07/30/2020, 2:10 PMSecond call of that effect will create second subscription(both are alive) and in future each subscription will call setUsers and will create more and more subscriptions@Akif Abasov [JB], I didn't fully understand you in the last paragraph. I'm learning and I'm noob with React, but as I have seen in the documentation: Dependency array checks with previous value and it only renders when there is a change in that value. But in my case it is rendering when I make a change in
messages
and users
. When only would have to do it in users
.
I created a Scratch Project with React not Kotlin-react with a similar case and there it works without multiple renders.Akif Abasov [JB]
07/30/2020, 2:12 PMAkif Abasov [JB]
07/30/2020, 2:14 PMfrank
07/30/2020, 2:43 PMsocket.on ("loginUser") {}
only when my server calls it at "*loginUser*".
If I remove setMessages (items)
statement works perfectly without multiple renderings. I understand what problem should be that the dependency array is not being defined correctly.Akif Abasov [JB]
07/30/2020, 3:11 PM