R
R
rockwell3232021-01-11 23:58:05
React Native
rockwell323, 2021-01-11 23:58:05

Why does a React Native app freeze?

I am writing the first application in react native, I created a websocket server, I receive data once a minute, everything seems to work fine, but then the application freezes and I can’t return to the main page
. Here is the server code:

io.on("connection", socket => {
  console.log(`Connected: ${socket.id}`);

  socket.on('disconnect', () => console.log(`Disconnected: ${socket.id}`));

  async function requestMain(){
    var result = await mainFunction();
    await socket.emit("info", result);
    setTimeout(requestMain, 60000);
  };
  requestMain();
});

server.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);
});

Here is the code with react native :
var socket;
const [isLoading, setLoading] = useState(true);
const [data, setData] = useState([]);

useEffect(() => {
   socket = io("http://example/", {
       transports: ['websocket']
   });

   socket.on("info", info => {
      setData(info);
      setLoading(false);
   });

   return () => {
      socket.disconnect();
   };
}, [isLoading]);
//Далее идет парсинг JSON строки и строится DOM


Most likely I didn’t understand useEffect, what could be the problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question