[项目]Go语言与React实现图片浏览器 Day 6

点击翻页按钮会更新current,而直接点击图片列表的图片并没有更新这个状态,今天把这个功能增加上,并且把当前页保存在了浏览器本地,使用localStorage的getItem和setItem方法实现。

PhotoItem.js

import React from 'react';
import '../config';
class PhotoItem extends React.Component {
    constructor(props) {
        super(props);
        console.log("constructor()");
        this.state = {
            serverUrl: global.constants.serverUrl,
            photoItem: props,
            showUrl: "",
            current: 0,
            nextIndex: 0,
            preIndex: 0,
            total: 0,
        }
        this.handlePrePhoto = this.handlePrePhoto.bind(this);
        this.handleNextPhoto = this.handleNextPhoto.bind(this);
        this.changePhoto = this.changePhoto.bind(this);
    }

    componentWillMount() {
        let current = localStorage.getItem('current');
        if (current !== isNaN){
            console.log("getCurrent:"+current);
            this.setState({current: parseInt(current)})
        }
    }
    componentDidMount (){
        const {photoItem} = this.state.photoItem;
        let current =  this.state.current;
        if ( photoItem.length >0){
            let index = 0;
            if (current!==0 && photoItem.length>current ){
                index = current;
            }
            var item = photoItem[index];
            this.setState({
                showUrl : this.state.serverUrl+'show/'+item.FilePath +'/'+item.FileName,
                total : photoItem.length,
            })            
        }
    }

    handlePrePhoto(){
        var current = 0;
        if (this.state.current>0){
            current = this.state.current-1;
        }
        this.changePhoto(current);

    }
    handleNextPhoto(){
        var current = 0;
        if ( this.state.total >this.state.current){
            current = this.state.current+1;
        }
        this.changePhoto(current);
    }

    changePhoto(current){
        const {photoItem} = this.state.photoItem;
        console.log(current);
        var item = photoItem[current];
        if (this.state.total > current && current>=0){
            this.setState({
                current:current,
                showUrl : this.state.serverUrl+'show/'+item.FilePath +'/'+item.FileName,

            });
            localStorage.setItem('current',current);

        }else{
            console.log(" No photo.");
        }
    }

    render(){
        const {photoItem} = this.state.photoItem;

        return (
            <div className="photoMain">
                <div>total:{this.state.total},current:{this.state.current}</div>
                <div className="photo-detail">
                <ul>
                    <li onClick={this.handlePrePhoto.bind(this)}>                       
                        PrePhoto                        
                    </li>
                    <li>
                    <img src={this.state.showUrl} alt={this.state.showUrl}></img>
                    </li>
                    <li onClick={this.handleNextPhoto.bind(this)}>
                        NextPhoto
                    </li>
                </ul>
                </div>
                <div className="photo-list">            
                <ul>
                    <li>left</li>
                    {photoItem.map((item,index) => (
                        <li key={item.FileName} onClick={this.changePhoto.bind(this,index)}>
                            <img src={this.state.serverUrl+'show/'+item.FilePath +'/'+item.FileName} alt={item.FileName}/>
                        </li>               
                    ))}
                    <li>right</li>
                </ul>                
            </div> 
          </div>
        );
    }
}

export default PhotoItem;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值