Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6211

ReactJS change background image dynamically doesn't work

$
0
0

I am new to reactJS. I want to create a component to change background images dynamically. I just write a javascript to update background image every 5 seconds but it doesn't work. These images are local images, I want to loop them and display them. Would you please take a look? Thank you very much!

import * as React from 'react';
import * as image from './resources';

const TIME_TICK_PERIOD = 5000;
var images =['resources/yellowstone.jpg','resources/yellowstone2.jpg','resources/yellowstone3.jpg'];

export class BackgroundImage extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            index:0
        };
    }

    componentDidMount() {
        this.timerID = setInterval(() => this.tick(), TIME_TICK_PERIOD);
    }

    componentWillUnmount() {
        clearInterval(this.timerID);
    }

    tick() {
        let idx = this.state.index;
        if(idx >=images.length-1) {
            this.setState({
                index: 0
            });
        } else {
            this.setState({
                index: idx+1
            })
        }
    }

    render() {
        return (
            <div className="hints-on-home-screen">
                <div
                    style={{
                        position: absolute,
                        backgroundImage: `url(${images[this.state.index]}`)
                    }}
                />
            </div>
        );
    }
}


Viewing all articles
Browse latest Browse all 6211

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>