TypeError: Undefined не является объектом (оценка this.state.videos.map) - PullRequest
1 голос
/ 10 июля 2020
• 1000 папку, которую я создал. Я получаю сообщение об ошибке typeError.

state = {
  index: null
}

componentWillMount() {
  RNFS.readDir(RNFS.ExternalStorageDirectoryPath + "CustomFolder Videos")
    .then((result) => {
      console.log('GOT RESULT', result);
      return Promise.all([RNFS.stat(result[0].path), result[0].path]);
    })
    .then((statResult) => {
      let videos = [];
      var allowedExtensions = /(\.avi|\.mp4|\.mov|\.wmv|\.avi)$/i;
      statResult.forEach(item => {
        if (item.isFile() && !allowedExtensions.exec(item.originalFilepath)) {
          videos.push(item);
        }
      });
      console.log(videos)
    })
}

setIndex = (index) => {
  if (index === this.state.index) {
    index = null;
  }
  this.setState({
    index
  })
}


render() {
  return ( <
    View style = {
      styles.container
    } >
    <
    ScrollView contentContainerStyle = {
      styles.scrollview
    } {
      ...this.state.videos.map((p, i) => {
        const isSelected = i === this.state.index;
        const divide = isSelected && this.share === true ? 1 : 3;
        return ( <
          Video source = {
            {
              uri: videos
            }
          }
          style = {
            {
              opacity: i === this.state.index ? 0.5 : 1,
              width: width / divide,
              height: width / divide
            }
          }
          key = {
            i
          }
          underlayColor = 'transparent'
          onPress = {
            () => this.setIndex(i)
          }
          ref = {
            ref => {
              this.player = ref;
            }
          } // Store reference
          onError = {
            this.videoError
          } // Callback when video cannot be loaded
          />


        )
      })
    } >

    <
    /ScrollView> <
    /View>
  );
}

}

Ответы [ 2 ]

1 голос
/ 10 июля 2020

измените методы рендеринга, как показано ниже, это будет работать,

 state = {
      index: null,
      videos:[]
    }

render() {
    return (
        <View style={styles.container}>
            <ScrollView
                contentContainerStyle = {styles.scrollview}
                {
                    this.state.videos&& this.state.videos.length>0 &&        this.state.videos.map((p, i) => {
                        const isSelected = i === this.state.index;
                        const divide = isSelected && this.share === true ? 1 : 3;
                        return(
                            <Video
                                source={{uri: videos}}
                                style={{opacity: i === this.state.index ? 0.5 : 1, width: width/divide, height: width/divide}}
                                key={i}
                                underlayColor='transparent'
                                onPress={() => this.setIndex(i)}
                                ref={ref => {
                                    this.player = ref;
                                  }} // Store reference
                                  onError={this.videoError} // Callback when video cannot be loaded
                            />

                            
                        )
                    })
                }
            >

            </ScrollView>
        </View>
    );
}

}

дело в том, что this.state.videos пуст, пока ответ api не получит

0 голосов
/ 10 июля 2020

видео не определяется в состоянии. Сначала вам нужно инициализировать состояние, а затем setstate для обновления значений.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...