javascript - 在 React 中重新加载实时图像而不闪烁

标签 javascript reactjs react-toolbox

我想在 React 中无限期地在静态 URL 重新加载图像。通过一些搜索,我得出了以下不太理想的解决方案。它有效,但我想消除图像加载的闪烁。我意识到问题是组件被重新渲染,然后图像加载。我见过几个示例,其中一个使用两个图像作为占位符,另一个使用隐藏加载,直到它使用 onLoadsetState 加载,但它们都假设一个有限的数字图像。我怎样才能让它显示 CardMedia 中的最后一张图像,直到加载新图像,然后每五秒更换一次而不闪烁?

import React from 'react';
import ReactDOM from 'react-dom';

import { Card, CardMedia, CardTitle } from 'react-toolbox/lib/card';

const LIVE_IMAGE = 'https://cdn-images-1.medium.com/max/1600/1*oi8WLwC2u0EEI1j9uKmwWg.png';

class LiveImageCard extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      liveImage: null
    };
  }

  componentDidMount() {
    this.interval = setInterval(
      () => this.setState({
        liveImage: `${LIVE_IMAGE}?${new Date().getTime()}`,
      }),
      5000
    );
  }
  componentWillUnmount() {
    clearInterval(this.interval);
  }

  render() {

    return (
      <Card style={{width: '350px'}}>
        <CardTitle title="Live Image" />
        <CardMedia
          aspectRatio="wide"
          image={this.state.liveImage}
        />
      </Card>
    );
  }
}

ReactDOM.render(
  <LiveImageCard />,
  document.getElementById('root'),
);

最佳答案

对我来说,删除 <Image> Prop ,resizeMode={"contain"}解决了 Android 上的闪烁问题。

关于javascript - 在 React 中重新加载实时图像而不闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49868915/

相关文章:

javascript - 如何对 jquery 选择的插件进行自动完成搜索?

javascript - NodeJs : Multiple loop of HTTP requests execute simultaneously

javascript - 使用 React JS 使 Glyphicon 日历图标可点击

reactjs - 使用 webpack 为 SCSS 添加变量

reactjs - React-toolbox如何正确包含样式

javascript - 带有必填字段的 React-Toolbox 对话框表单

javascript - 为什么我的折线图在 chartjs 中倒退?

javascript - JQuery 语法检查器

javascript - 如何在 react js 应用程序中插入 hubspot 表单?

javascript - 在自定义日历中显示获取的日期React Native