Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

javascript - React Native how call api or run a function indefinitely

I am pulling api data manually and need to check for changes instantly trying to figure the best way but my code only pulls the data once. how do I get it to pull data constantly or indefinitely

import React, {useState, useEffect} from 'react';

const Engine = () => {
  const [information, setInformation] = useState(null);
  useEffect(() => {
    fetch('http://127.0.0.1:63726')
      .then((response) => response.json())
      .then((json) => {
        setInformation(json[0]);
        console.log('You can see the info here', information);
      })

      .catch((error) => console.error(error));
  }, [information]); 
  return <></>;
};
export default Engine;

I need react to check api none stop and update the state with any changes

question from:https://stackoverflow.com/questions/65835143/react-native-how-call-api-or-run-a-function-indefinitely

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use a .setInterval or requestAnimationFrame if you'd like


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...