简体   繁体   English

React Native中与数组相关的问题

[英]Array related issue in React Native

import React from 'react';
import {
    View,
    ScrollView,
    Image,
    Dimensions,
    TextInput,
    Text,
    StatusBar,
    TouchableHighlight,
    Linking,
    Keyboard,
    CameraRoll,
    KeyboardAvoidingView
} from 'react-native';
import {connect} from 'react-redux';
import {ActionCreators} from '../redux/actions';
import {bindActionCreators} from 'redux';
import Colors from '../constants/Colors';
import api from '../api';
import {
    getEmailAddress,
    showError,
    renderMessageBar,
    registerMessageBar,
    unregisterMessageBar
} from '../utils/ComponentUtils';
import {
    regularHeader,
    mainHeader
} from '../utils/Headers';
import {NavigationActions} from 'react-navigation';
import {SelectionLimitDialog} from '../utils/Dialogs';
import {ifIphoneX, isIphoneX} from 'react-native-iphone-x-helper';
import {SafeAreaView} from 'react-navigation';

// specific component imports.
import {List, ListItem} from 'react-native-elements'
import {Button} from 'react-native-elements'
import Loader from '../components/Loader';
import LoaderError from '../components/LoaderError';
import SelectedPhoto from '../components/SelectedPhoto';

class MultiSafeeScreen extends React.Component
{
    static navigationOptions = ({navigation}) => {
        const {params} = navigation.state;
        const isIncome = params ? params.isIncome : false;
        const notificationAction = params ? params.notificationAction : () => {};
        const isShowBack = params ? params.isShowBack : false;

        return mainHeader({
            isShowBack: isShowBack,
            backAction: () => navigation.goBack(),
            notificationAction: () => notificationAction(),
            income: isIncome
        });
    };

    constructor(props) {
        super(props);

        this.selectedPhotos = [];

        this.state = {
            photos: null,
            loader: {
                loading: 0,
                message: "Loading photos..."
            }, 
            selectedPhotos: [],
            supportLength: 5
        };

        this.props.navigation.setParams({notificationAction: this.onNotification, isIncome: this.props.isNewNotifications});
    }

    UNSAFE_componentWillReceiveProps(newProps) {
        if (newProps.isNewNotifications !== this.props.isNewNotifications) {
            this.props.navigation.setParams({notificationAction: this.onNotification, isIncome: newProps.isNewNotifications});
        }          
    }

    componentDidMount() {
        registerMessageBar(this.refs.alert);

        let options = {
            first: 30,
            assetType: 'Photos',           
        }

        CameraRoll.getPhotos(options)
        .then(r => {
            this.setState({
                photos: r.edges,
                loader: {
                    loading: 1,
                    message: "Loading photos..."
                },                                 
            });
        })
        .catch((err) => {
             //Error Loading Images
        });

        StatusBar.setHidden(false);
    }

    componentWillUnmount() {
        unregisterMessageBar();

        this.props.setSelectedPhotos(0);
    }

    onNotification = () => {
        this.props.setNewNotifications(false);
        this.props.navigation.navigate("Notifications");
    }

    closeKeyboard = () => {
        Keyboard.dismiss();
    }

    onSelectPhoto = (photo, index) => {
        let photos = new Set([...this.selectedPhotos]);
        let len = photos.size + 1 ;
        console.log('len = ', len)

        if (len > this.state.supportLength) {
            this.limitDialog.open();
        }
        else if (len === this.state.supportLength) {
            this.setState({selectedPhotos: this.selectedPhotos});
            this.props.setSelectedPhotos(len); 
            console.log('setSelectedPhotos= ', this.props.setSelectedPhotos)
        }
        else {
            photos.add(photo);
            this.selectedPhotos = Array.from(photos);
        }
    }

Hey I would appreciate some guidance with my code , particularly with onSelectPhoto function where I try to run through an array, it works, but only partially, it doesn't shows an limit message when user selects more than 5 photos which is the limit and when going to next screen it shows only 4 photos out of 5.I guess the problem is that when the value equals to 5 I dont add anything to the array and therefore his value stays 5 because I count the size according to the size of the array. 嘿,我希望我的代码提供一些指导,特别是onSelectPhoto函数,在该函数中我尝试遍历数组,它可以工作,但仅部分实现,当用户选择5张以上的照片时,它不会显示限制消息,这是限制和转到下一个屏幕时,它仅显示5张照片中的4张。我想问题是,当值等于5时,我没有向数组中添加任何内容,因此他的值保持为5,因为我根据大小来计算大小数组。 What would be the right way to add to the array so the value will change accordingly? 什么是添加到数组的正确方法,以便值将相应地更改? full code: https://pastebin.com/m0gxUCBt 完整代码: https//pastebin.com/m0gxUCBt

Assuming that your supportLength is 5 that should do the work. 假设您的supportLength为5,则应完成工作。 Try debugging you code using console.log to see the value in the variable len and this.state.supportLength. 尝试使用console.log调试代码,以查看变量len和this.state.supportLength中的值。

Another way of debugging witch I prefer is using breakpoints on you code to see you actual flow of execution of you code. 我更喜欢调试女巫的另一种方法是在代码上使用断点,以查看代码的实际执行流程。

If you don't know how to do it. 如果您不知道该怎么做。 Check this article https://facebook.github.io/react-native/docs/debugging.html 检查本文https://facebook.github.io/react-native/docs/debugging.html

onSelectPhoto = (photo, index) => {
    let photos = new Set([...this.selectedPhotos]);
    let len = photos.size ;
    console.log('len = ', len)

    if (len >= this.state.supportLength) {
        this.limitDialog.open();
    }
    else if (len === this.state.supportLength) {
        this.setState({selectedPhotos: this.selectedPhotos});
        this.props.setSelectedPhotos(len); 
        console.log('setSelectedPhotos= ', this.props.setSelectedPhotos)
    }
    else {
        photos.add(photo);
        this.selectedPhotos = Array.from(photos);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM