Reactjs React路由器重定向和useEffect内存泄漏

Reactjs React路由器重定向和useEffect内存泄漏,reactjs,asynchronous,memory-leaks,axios,use-effect,Reactjs,Asynchronous,Memory Leaks,Axios,Use Effect,我有一个内存泄漏,我不知道是什么原因造成的。我有一个/login页面,在成功登录时路由到/。然后,我在n秒后重新路由到登录页面。同时,我在/页面上有一个Axios get请求。在打开控制台并意识到在重新路由过程中内存泄漏之前,一切看起来都很好。登录页面将出现,但控制台将停留在get请求上,并无限期地继续请求。这里有一些代码 const getData = React.useCallback(() => { const value = Axios.post("http://

我有一个内存泄漏,我不知道是什么原因造成的。我有一个/login页面,在成功登录时路由到/。然后,我在n秒后重新路由到登录页面。同时,我在/页面上有一个Axios get请求。在打开控制台并意识到在重新路由过程中内存泄漏之前,一切看起来都很好。登录页面将出现,但控制台将停留在get请求上,并无限期地继续请求。这里有一些代码

const getData = React.useCallback(() => {
    const value = Axios.post("http://localhost:3001/api/get-value",
    {user: userProp}).then((response) => {
        const recievedData = response.data;
        const dataValue = recievedData.map((val) => {
            return [val.value]
        })
        if (loading === true){
            setLoading(false);
        }
        return parseInt(dataValue);
    }).then((resp) => {
        setMoisture(resp)
        return resp
    })
    return value
}, [userProp, loading])

try{
    const payload = usePolling(function () {
        return Promise.resolve(getData())
      }, 4000);
}catch(e){
    console.log(e)
}
有效载荷功能

function usePolling(fetcher, interval) {
const [payload, setPayload] = React.useState(null);

React.useEffect(function () {
  // you must implement the error handling
  fetcher()
    .then(function (resp) {
      setPayload(resp)
    })
}, [fetcher]);

React.useEffect(function () {
  let timeoutId;
  
  function poll() {
    timeoutId = setTimeout(function () {
      // you must implement the error handling
      fetcher()
        .then(function (resp) {
          setPayload(resp)
          poll();
        })
    }, interval);
  }
  poll()
  
  return function () {
    clearTimeout(timeoutId);
  }
}, [fetcher, interval]);

return payload;
}

并呈现

if ((props.location.state) && timeDif <= .2) {
    return(
        <div className="App">
            <Switch>
            <Route path="/login" component={LoginComponent} />
            <Route path="/">
                <SensorComponent moisture={value} airValueObject={airValueObject}/>    
            </Route>
            </Switch>
        </div>
    );
}else{
    return(
        <div>
            <Route path="/login" component={LoginComponent} />
            <Route path="/">
                <Redirect to="/login" />
            </Route>
        </div>
    )
}
if((props.location.state)和&timeDif