【问题标题】:React Video Component not updating when state changes状态更改时反应视频组件不更新
【发布时间】:2021-10-08 05:54:51
【问题描述】:

我正在尝试创建一个视频播放器,但我遇到了这个问题。 在 React 中,我有一个从服务器获取 JSON 的页面,然后创建简单的 href 来更改视频。 HTML5 播放器作为一个组件引入,其中包含提供控件、src、描述等的对象的 prop。 我放置控制台日志以查看信息是否通过,当页面重新加载时状态是正确的,但是,视频没有反映状态变化。

我已经用尽了所有途径。任何帮助将不胜感激。

const FS5VideoPlayer = () => {
  //Generate the playList
  const [isLoaded, SetIsLoaded] = useState(false);
  const [error, SetError] = useState(null);
  const [items, SetItems] = useState([]);
  const [nowPlaying, SetNowPlaying] = useState("");


  // Needs to work on variable declaration and not hardcoded
  const url = 'http://localhost:8080/videos/videolist'

  useEffect(() => {
    fetch(url)
      .then(response => response.json())
      .then(json => {
        SetItems(json.videos);
        SetIsLoaded(true);
      })
      .catch(error => {
        SetError(error);
        SetIsLoaded(true);
      });

  }, []);
  if (error) {
    return <div>{error}</div>;
  }
  else if (!isLoaded) {
    return <div>Loading...</div>;
  }
  else {
    console.log(items[0].src);
    console.log("loading-nowPlaying", nowPlaying);
    if (nowPlaying === "") {
      SetNowPlaying(items[0]);
      console.log("Initial Load")
    }
    else {
      console.log("all good")
    }
  }

  function playVideo(videoObj) {
    console.log("videoObj", videoObj);
    SetNowPlaying(items[videoObj]);

    console.log("Now Playing", nowPlaying);

  }
  return (
    <>
      <div className="container-fluid">
        <HTML5Video
          videoInfo={nowPlaying}
        />
        <div>
          {
            // Map Video List 
            //

            items.map((videos, index) => {
              return (
                <div key={videos.id}>
                  <a href="#" onClick={() => playVideo(index)}>
                    {videos.title}
                  </a>
                </div>
              )
            }
            )}


        </div>
      </div>
    </>
  );
};

HTML5 播放器

const HTML5Video = ({ videoInfo }) => {
  const urlPath = () => {
    const hostname = window.location.hostname;
    const port = window.location.port;
    const myDocker = `http://${hostname}:${port}`
    const myDev = "http://localhost:8080";
    if (port === "3000") {
      return myDev;
    } else {
      return myDocker;
    }
  }
  console.log("HTML5", videoInfo);
  /**
   * Adding logic for chromecast button
   * Currently throwing a lot of erros. Revisit
   * https://stackoverflow.com/questions/44999267/how-to-chromecast-enable-a-website/49089116
   */


  return (
    <div>

      <h2>HTMLFiveVideo</h2>
      <video width="720" controls={videoInfo.defaultControls}>
        <source src={`${urlPath()}/videos/videofiles?videosrc=${videoInfo.src}`} />
        {console.log(urlPath())}
      </video>


    </div>
  );
};

export default HTML5Video;


【问题讨论】:

    标签: javascript reactjs use-effect use-state


    【解决方案1】:

    我遇到了类似的问题。我的解决方案是在向状态添加新值之前设置超时并在之前清除它。

    问题可能是因为 setState() 通常是异步的,这意味着当你想看到状态的变化时,它还没有更新。

    const watchNewVideo = (video) => {
    setTopVideo(null);
    setTimeout(() => {
      setTopVideo(video);
    }, 10);
    

    };

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-25
      • 1970-01-01
      • 2021-07-07
      • 2019-04-03
      • 2016-01-19
      • 2018-11-03
      • 1970-01-01
      • 2017-05-05
      相关资源
      最近更新 更多