簡體   English   中英

TypeError: Undefined is not an object(評估'this.state.videos.map')

[英]TypeError: Undefined is not an object (evaluating 'this.state.videos.map')

我試圖在我的 react native 應用程序中獲取保存在設備上的視頻文件的網格列表作為視頻庫,但我認為有些地方做得不對,我使用 react-native-fs 從我創建的自定義文件夾中獲取文件. 我收到一條 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> ); } }

改變你像下面這樣的渲染方法,這將起作用,

 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 響應將得到

視頻未在 state 中定義。 您需要先初始化 state 然后 setstate 來更新值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM