reactjs - 视频源未在 useState() 上更新

标签 reactjs video state refresh

import React, { useState, useEffect } from 'react';
import rainClip from '../videos/rain.mp4';
import sunnyClip from '../videos/sunny.mp4';
import dullClip from '../videos/cloudy_black.mp4'
import '../App.css'
export default function Background({ weather }){
  const [clip, setClip] = useState(sunnyClip)
  console.log(clip)
  console.log('Background loaded')
  useEffect(() =>{
    if (weather[0].description.includes('дождь')){
      setClip(rainClip)
      console.log('Setted rain')
    }
    if (weather[0].description.includes('пасмурно')){
      setClip(dullClip)
      console.log('Setted dull')
    }
    if (weather[0].description.includes('ясно')){
      setClip(sunnyClip)
      console.log('Setted sun')
    }
  }, [weather])
 
    return(
        <video autoPlay loop muted
        style={{
          height: '100%',
          zIndex: '-1',
          left: '50%',
          transform: 'translate(-50%, 0%)',
          position: 'absolute'
        }}
        >
        <source src={clip} type="video/mp4" />
      </video>
    )
}

我正在尝试制作一个具有与当前天气相符的动态背景的天气应用程序。但是,尽管剪辑状态已更新,但我的元素不会更新视频。 是否可以动态更改视频背景?

最佳答案

有同样的问题,在阅读另一个线程后设法解决它:Video displayed in ReactJS component not updating

在 React 中,我们必须更改整个

...
return(
        <video autoPlay loop muted
        style={{
          height: '100%',
          zIndex: '-1',
          left: '50%',
          transform: 'translate(-50%, 0%)',
          position: 'absolute'
        }}
        src={clip}
        type="video/mp4"
        />
        
   
    );

关于reactjs - 视频源未在 useState() 上更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67678031/

相关文章:

reactjs - React Native - 使用图表套件时出错(执行 UI block 时抛出异常 : __NSCFNumber firstObject: unrecognized selector sent to instance)

video - 哪种格式最适合用于高质量的在线音频和视频?

objective-c - 找不到 MediaPlayer/Mediaplayer.h 文件

PHP使用ffmpeg将任何视频转换为MP4

oop - 在装饰器模式中添加状态

javascript - React 钩子(Hook)中的 `useState(null)[1]` 是什么?

javascript - 如何在reactjs中将表单单选按钮重置为未选中?

javascript - 如果任何依赖项在路径中有 "Could not locate module",则开 Jest "src"

c++ - QTcpSocket 状态始终连接,即使拔掉以太网线

javascript - 如何在同一组件中提供和使用上下文?