Javascript 如何防止axios在状态更新前发布

Javascript 如何防止axios在状态更新前发布,javascript,reactjs,state,blocking,Javascript,Reactjs,State,Blocking,我有一个问题,post请求没有发送更新的currentVideo状态,因为setState似乎是非阻塞的。如何让axios等待状态设置 const nextVideo = () => { if (videoList.indexOf(currentVideo) < videoList.length - 1) { setCurrentVideo(videoList[videoList.indexOf(currentVideo) + 1]); setLoad

我有一个问题,post请求没有发送更新的currentVideo状态,因为setState似乎是非阻塞的。如何让axios等待状态设置

const nextVideo = () => {
    if (videoList.indexOf(currentVideo) < videoList.length - 1) {
      setCurrentVideo(videoList[videoList.indexOf(currentVideo) + 1]);
      setLoading(true);
      axios
        .post(`${BACK_PORT}/videos/download`, currentVideo)
        .then(function (response) {
          if (response.data) {
            setLoading(false);
          } else {
            console.log("waiting...");
          }
        })
        .catch(function (error) {
          alert(error)
        });
    } else {
      alert("This is the last video");
    }
  };
const nextVideo=()=>{
if(videoList.indexOf(currentVideo)
使用变量并将当前视频值存储在其中。然后在两个位置使用该变量,即设置状态和进行axios调用

const nextVideo = () => {
    if (videoList.indexOf(currentVideo) < videoList.length - 1) {
      let currentVideo = videoList[videoList.indexOf(currentVideo) + 1] //change here
      setCurrentVideo(currentVideo );
      setLoading(true);
      axios
        .post(`${BACK_PORT}/videos/download`, currentVideo)
        .then(function (response) {
          if (response.data) {
            setLoading(false);
          } else {
            console.log("waiting...");
          }
        })
        .catch(function (error) {
          alert(error)
        });
    } else {
      alert("This is the last video");
    }
};
const nextVideo=()=>{
if(videoList.indexOf(currentVideo)
设置状态是完全异步的,您是否考虑过在
currentVideo
状态中使用?这是否回答了您的问题?