阵列状态未设置

时间:2018-10-13 15:48:05

标签: android reactjs react-native

我正在尝试设置数组的状态,但没有设置它,我正在映射从react-native-image-crop-picker获取的结果,因为我映射了setState数组,但是它没有设置状态,它总是空的,设置完数组的状态后,我想通过它映射并显示为图像,请问我可能做错了什么,为什么数组没有呢?设置

import ImagePicker from 'react-native-image-crop-picker';

imageUpload(){
    ImagePicker.openPicker({
        multiple: true,
        cropping: true,
        mediaType: 'photo'
      }).then(images => {
        this.setState({
            images_array: images.map(i => {  
                console.log('received image', i);
                return {uri: i.path};
            })
        }, console.log(this.state.images_array));
    }).catch(e => console.log(e));
} 
constructor(props) {
    super(props);
    this.state = {
        upload: false,
        home: true,
        category: false,
        amount: false,
        no: 0,
        cat_id: '', 
        images_array: [],
        description: '',
        amount: '',

    };
}
images = this.state.images_array.map((image, index)=>{
        <Image resizeMode="contain" style={{width: 38,
    height: 38,
    borderRadius: 19}}
               source={{uri: image.uri}}/>});
<ScrollView showsVerticalScrollIndicator={false}>
                        <Text style={{
                            fontFamily: 'mont-medium', fontSize: 14, color: '#000', marginTop: 39, textAlign: 'center' }}>
                         Upload at least one picture{'\n'} of your product
                        </Text>
                        <TouchableNativeFeedback onPress={this.imageUpload.bind(this)}>
                        <View style={styles.multi}>
                            <View style={{height: 111, width: 123,alignSelf: 'center',}}>
                                <Image
                                    resizeMode="contain"
                                    style={{alignSelf: 'center', flex: 1}}
                                    source={require('../upload.png')}/></View>
                            <Text style={{fontFamily: 'mont', fontSize: 12,
                                color: '#b3b1b1',alignSelf: 'center',}}>
                                Attach photos...
                            </Text>
                        </View>
                        </TouchableNativeFeedback>
                        <View style={styles.does}>
                        {images}
                </View>
                        </ScrollView>

2 个答案:

答案 0 :(得分:1)

尝试一下

  .then(images => {
    const imagesArray = [];
    if(images){
    images.map(i => {  
           imagesArray.push({uri: i.path});
     })
     }
    this.setState({
        images_array: imagesArray
    });
 })

答案 1 :(得分:0)

您的.map不返回任何值

代替

class MyClass
  def my_method url
    # do some operation on url
    yield url
  end
end

尝试

m = MyClass.new
m.my_method("url") do |url|
  # do some operation on url
end

或这个

images = this.state.images_array.map((image, index)=>{
        <Image resizeMode="contain" style={{width: 38,
    height: 38,
    borderRadius: 19}}
               source={{uri: image.uri}}/>});