0

Hy, I'm working on react-native to integrate WebSocket. I'm setting the list state const [messagesList, setMessagesList] = useState([]) when I set the messages in the list but facing an issue which is that when I map the list on the second time it also display the pervious data like on first map it print

Hello Adil
How're you?

when I get the new data it also map it again:

Hello Adil // first map
How're you? // first map
//second map
Hello Adil
How're you?
new message
new message

I want my old messages to remain on-screen but don't display them again on the screen here's the code: Setting the state

 setMessagesList((messagesList)=>[...messagesList,{'msg':type,'user':data.text}])

map the messageList

messagesList.map((msg,index)=>{
      return(
        <View style={{flexDirection:'row',
        justifyContent:msg.user == 'adilijaz.09@gmail.com' ? 'flex-start' : 'flex-end'

        }} > 
        <Text>{msg.msg}--{msg.user}</Text>
      </View> 
      )
      })
Adil Ijaz
  • 312
  • 1
  • 5
  • 16

0 Answers0