【问题标题】:Can't efficiently scroll iniside React native application无法在 React 本机应用程序内有效滚动
【发布时间】:2017-12-27 23:22:13
【问题描述】:

我只是一个初学者,一直在致力于一个需要尽快完成的项目。我通过 API 获取了超过 5000 个数据列表,但是,该列表在滚动时效率不高,因此会破坏应用程序。我一直在 React Native 中搜索 FlatList 组件,我无法在我的项目中正确实施。 有人可以提供有关如何解决此问题的建议。我在下面附上了我的源代码 -

import React, {Component} from 'react';
import {Text, View, FlatList, ScrollView } from 'react-native';
import axios from 'axios';
import GalleryDetail from './GalleryDetail';

class GalleryList extends Component {

state = { photos: []};

componentWillMount() {
    axios.get('http://jsonplaceholder.typicode.com/photos')
    .then(response => this.setState({ photos: response.data })).
    catch((error)=> console.warn("fetch Error: ", error));
}

renderPhotos() {
    return this.state.photos.map( photos => 
        <GalleryDetail key={photos.id} photos= {photos}/>
    );
}


render () {
    return (
        <View>
            <ScrollView>
                {this.renderPhotos()}
            </ScrollView>
        </View>
    );
}
}

export default GalleryList;

我的 GalleryDetail 在哪里

import React, {Component} from 'react';
import { Text, View, Image } from 'react-native';
import Card from './Card';
import CardSection from './CardSection';

const GalleryDetail = (props)=> {
    return (
        <Card>
            <CardSection style = {styles.headerContentStyle}>
                <Image
                style={styles.thumbnailStyle}
                source = {{ uri: props.photos.thumbnailUrl}}/>
                <Text style= {styles.textStyle}>{props.photos.title}</Text>
            </CardSection>
        </Card>
    );
};

const styles = {
    headerContentStyle: {
       flexDirection: 'column',
       justifyContent: 'space-around'
   },

   thumbnailStyle: {
       height: 50,
       width: 50
   },

    textStyle: {
       textAlign: 'right',
       marginLeft: 3,
        marginRight: 3,
    }
}

export default GalleryDetail;

很抱歉没有提供正确的代码段。请帮忙

【问题讨论】:

    标签: reactjs listview react-native redux react-native-flatlist


    【解决方案1】:

    首先,您应该按照文档说明在componentDidMount 中进行网络调用。

    其次,不要使用 ScrollView,因为它性能不佳,并且使用 FlatList 时,您不再需要 ScrollView。

    第三,更新 GalleryDetail 以使用 props.photo 而不是 props.photos,因为您将单个对象传递给每一行(复数使其不直观)。并通过照片对象中的item属性访问数据:

    const GalleryDetail = (props)=> {
        return (
            <Card>
                <CardSection style = {styles.headerContentStyle}>
                    <Image
                    style={styles.thumbnailStyle}
                    source = {{ uri: props.photo.thumbnailUrl}}/>
                    <Text style= {styles.textStyle}>{props.photo.title}</Text>
                </CardSection>
            </Card>
        );
    };
    

    最后使用下面的sn -p

      render() {
        if (!this.state.photos) {
          return <ActivityIndicator/>;
        }
    
        return (
          <FlatList
            data={this.state.photos}
            keyExtractor={this.keyExtractor}
            renderItem={this.renderPhoto}
          />
        );
      }
    
      keyExtractor = (photo, index) => photo.id;
    
      renderPhoto = ({item}) => {
        return < GalleryDetail photo={item} />;
      };
    

    【讨论】:

    • 嗨@Carlos,非常感谢您的回复。它真的帮了我很多。根据您的建议,我实施了第一个和第二个意见。然而,第三,我只想说使用 photos 作为关键字而不是 photo 的原因是由于我使用的 URL,因为它在其中使用了照片.但是,在尝试这两种方法时,它不会在屏幕上显示内容,而是在屏幕上添加带有所有样式的内容主体。如果您能就如何处理这种情况向我提供更多建议,那就太好了。
    • 我刚刚在您的代码中检查了您的 API 响应,并且网址不是 https。这不会在 iOS 中加载,它在文档中:facebook.github.io/react-native/docs/…。它只适用于 Android。
    • 在实现所有在 sn-p 中描述的建议和代码后,它不会导致在应用程序中显示图像和文本(来自 GalleryDetail)。我无法弄清楚原因,因为没有弹出错误。是的,我现在只想将 API 链接用于 android 应用程序,因为我需要对 react native 进行一些实践经验,但我不想离开这个地方。 @卡洛斯
    • 我对 renderPhoto 做了些微改动,因为 FlatList 将每个项目包装在一个项目属性中。它现在应该可以工作了。
    • 哇!!它有效!非常感谢您始终如一的帮助和建议@Carlos。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 2019-12-10
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 1970-01-01
    • 2019-09-30
    相关资源
    最近更新 更多