设为首页 加入收藏

TOP

React Native 开发豆瓣评分(八)首页开发(二)
2019-09-17 17:39:28 】 浏览:53
Tags:React Native 开发 评分 首页
('search')}> <View style={styles.searchView}> <Icon name='search1' size={px(30)} color='#ccc' /> <Text style={styles.searchText}>搜索</Text> </View> </TouchableWithoutFeedback> </View> {sections.map((list, index) => ( <View key={index} style={styles.list}> <ItemsHeader title={list.title} onPress={() => navigation.push('List', { data: list })} /> <FlatList horizontal={true} data={list.data} keyExtractor={(item, index) => 'item' + index} ListEmptyComponent={() => <MoviesItemPlaceholder />} renderItem={({ item, index }) => ( <View style={{ marginRight: index !== showing.length - 1 ? px(16) : px(30), marginLeft: index === 0 ? px(30) : 0 }}> <MoviesItem data={item} /> </View> )} /> </View> ))} </ScrollView> </View> ); } } const select = (store) => { return { value: store.num.value, } } export default connect(select)(Home); const styles = StyleSheet.create({ page: { flex: 1, backgroundColor: '#fff' }, search: { backgroundColor: '#00b600', height: px(80), alignItems: 'center', justifyContent: 'center' }, searchView: { height: px(50), width: px(710), borderRadius: px(8), backgroundColor: '#fff', flexDirection: 'row', justifyContent: 'center', alignItems: 'center' }, searchText: { fontSize: px(26), color: '#ccc', marginLeft: px(6) }, list: { marginBottom: px(30) } });

四、缓存列表数据

当处于弱网环境时,打开应用,可能会显示很久的占位图,此时我们可以将列表数据缓存至本地,每次进入应用先展示本地缓存的数据,然后请求数据,替换本地数据。

此时,就可以使用 redux 了。

编译 reducer

为了目录整清晰点(让 redux 相关的代码文件都存储于 store 目录下),将 src 目录下的 reducer 目录移动到 store 目录下,并在 reducer 目录创建 list.js。

const initList = {
    showing: [],
    hot: [],
    tv: [],
    variety: [],
    book: [],
    music: []
}

const setListState = (state = initList, action) => {
    switch (action.type) {
        case 'showing':
            return {
                ...state,
                showing: action.data
            }
        case 'hot':
            return {
                ...state,
                hot: action.data
            }
        case 'tv':
            return {
                ...state,
                tv: action.data
            }
        case 'variety':
            return {
                ...state,
                showing: action.data
            }
        case 'book':
            return {
                ...state,
                book: action.data
            }
        case 'music':
            return {
                ...state,
                music: action.data
            }
        default:
            return state;
    }
}

export default setListState;

在 reducer 的 index.js 中导入 setListState;

...
import setListState from './list';
...
export default combineReducers({
    ...
    list: setListState
    ...
});

修改 store/index.js 的路径引入

import reducer from './reducer';

编辑 action

将之前src下的 action 目录删除,在 store 目录下创建 action.js。

export function login(data) {
    return {
        type: 'login',
        data
    }
}

export function logout(data) {
    return {
        type: 'logout',
        data
    }
}

// set 首页列表数据
export function setShowing(data) {
    return {
        type: 'showing',
        data
    }
}
export function setHot(data) {
    return {
        type: 'hot',
        data
    }
}
export function setTv(data) {
    return {
        type: 'tv',
        data
    }
}
export function setVariety(data) {
    return {
        type: 'variety',
        data
    }
}
export function setBook(data) {
    return {
        type: 'book',
        data
    }
}
export function setMusic(data) {
    return {
        type: 'music',
        data
    }
}

编辑首页

导入修改 store 的方法:

import { setShowing, setHot, setTv, setVariety, setBook, setMusic } from '../store/action';

页面获取 store 存储的数据:

const select = (store) => {
  return {
    showing: store.list.showing,
    hot: store.list.hot,
    tv: store.list.tv,
    variety: store.list.variety,
    book: store.
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇vue-property-decorator使用指南 下一篇想精通正则表达式 这几个正则表达..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目