Nak*_*kib 10 javascript navigation reactjs react-native
我正在开发一个使用复合实验导航(CardStack + Tabs)和redux进行状态管理的本机应用程序.我能够创建基于Tab的导航,但我现在面临的问题是当我在选项卡之间切换时,组件已卸载并且每次都重新渲染.
问题
让我们说我已经滚动了几个帖子,当我更改Tab时,它将从顶部开始.(解决方法可能是在redux状态下存储滚动位置).
这是我用于导航选项卡实验导航的示例代码
您必须更改 TabBar 的方法。
基本上,您希望每个选项卡都有导航器,因此您可以为每个选项卡提供路由堆栈,并使用一个导航器来包含选项卡(TabBar)。您还想为 TabBar 设置初始路由堆栈并在这些路由之间跳转。
了解导航器方法之间的差异非常重要。
当您pop
路由时,它会被卸载,并且活动索引会移至最后一个。由于最后一个保持在状态,它将恢复为之前渲染的状态(很可能,它可能会发生道具更改)。再次转到popped
路线将完全重新渲染场景(这种情况发生在您身上)。
当你push
路由时,没有任何东西被卸载,新的路由被安装。
当您jumpToIndex
进行路线时,再次没有任何未安装的内容,因此
在路线之间跳转会恢复场景原样(同样,如果道具发生变化,场景将被重新渲染)。
所以,我认为这是不正确的:
我能够创建基于选项卡的导航,但我现在面临的问题是,当我在选项卡之间切换时,组件被卸载并且每次都会重新渲染。
...您使用错误的导航操作卸载路线。
现在的不同之处在于,它NavigationCardStack
并不实际创建自己的状态,而是从外部传递的,这为您提供了很大的灵活性。还有一个好处是,您可以使用 Facebook 提供的减速器来执行常见操作(例如推送、弹出、jumpToIndex;它们是导航实用程序的一部分)。
您在这里有关于如何创建navigationState
和减少器的完整示例,所以我不会解释这一点,只是给出如何解决您的问题的想法。
import React from 'react';
import { NavigationExperimental, View, Text, StyleSheet } from 'react-native';
const {
CardStack: NavigationCardStack,
StateUtils: NavigationStateUtils,
} = NavigationExperimental;
const style = StyleSheet.create({
screen: {
flex: 1,
},
screenTitle: {
marginTop: 50,
fontSize: 18,
},
pushNewScreenLabel: {
marginVertical: 10,
fontSize: 15,
fontWeight: "bold",
},
goBackLabel: {
fontSize: 15,
},
tabBarWrapper: {
position: 'absolute',
height: 50,
bottom: 0,
left: 0,
right: 0,
top: null,
backgroundColor: 'grey',
flexDirection: 'row',
flex: 0,
alignItems: 'stretch',
},
tabBarItem: {
flex: 1,
justifyContent: 'center',
backgroundColor: 'red',
},
});
export class TabBar extends React.Component {
constructor(props, context) {
super(props, context);
this.jumpToTab = this.jumpToTab.bind(this);
// Create state
this.state = {
navigationState: {
// Active route, will be rendered as default
index: 0,
// "tab-s" represents route objects
routes: [
{ name: 'Tab1', key: '1' },
{ name: 'Tab2', key: '2' },
{ name: 'Tab3', key: '3' },
{ name: 'Tab4', key: '4' }],
},
};
}
jumpToTab(tabIndex) {
const navigationState = NavigationStateUtils.jumpToIndex(this.state.navigationState, tabIndex);
this.setState({ navigationState });
}
renderScene({ scene }) {
return <Tab tab={scene.route} />;
}
render() {
const { navigationState } = this.state;
return (
<View style={style.screen}>
<NavigationCardStack
onNavigate={() => {}}
navigationState={navigationState}
renderScene={this.renderScene}
/>
<View style={style.tabBarWrapper}>
{navigationState.routes.map((route, index) => (
<TabBarItem
key={index}
onPress={this.jumpToTab}
title={route.name}
index={index}
/>
))}
</View>
</View>
);
}
}
class TabBarItem extends React.Component {
static propTypes = {
title: React.PropTypes.string,
onPress: React.PropTypes.func,
index: React.PropTypes.number,
}
constructor(props, context) {
super(props, context);
this.onPress = this.onPress.bind(this);
}
onPress() {
this.props.onPress(this.props.index);
}
render() {
return (
<Text style={style.tabBarItem} onPress={this.onPress}>
{this.props.title}
</Text>);
}
}
class Tab extends React.Component {
static propTypes = {
tab: React.PropTypes.object,
}
constructor(props, context) {
super(props, context);
this.goBack = this.goBack.bind(this);
this.pushRoute = this.pushRoute.bind(this);
this.renderScene = this.renderScene.bind(this);
this.state = {
navigationState: {
index: 0,
routes: [{ key: '0' }],
},
};
}
// As in TabBar use NavigationUtils for this 2 methods
goBack() {
const navigationState = NavigationStateUtils.pop(this.state.navigationState);
this.setState({ navigationState });
}
pushRoute(route) {
const navigationState = NavigationStateUtils.push(this.state.navigationState, route);
this.setState({ navigationState });
}
renderScene({ scene }) {
return (
<Screen
goBack={this.goBack}
goTo={this.pushRoute}
tab={this.props.tab}
screenKey={scene.route.key}
/>
);
}
render() {
return (
<NavigationCardStack
onNavigate={() => {}}
navigationState={this.state.navigationState}
renderScene={this.renderScene}
/>
);
}
}
class Screen extends React.Component {
static propTypes = {
goTo: React.PropTypes.func,
goBack: React.PropTypes.func,
screenKey: React.PropTypes.string,
tab: React.PropTypes.object,
}
constructor(props, context) {
super(props, context);
this.nextScreen = this.nextScreen.bind(this);
}
nextScreen() {
const { goTo, screenKey } = this.props;
goTo({ key: `${parseInt(screenKey) + 1}` });
}
render() {
const { tab, goBack, screenKey } = this.props;
return (
<View style={style.screen}>
<Text style={style.screenTitle}>
{`Tab ${tab.key} - Screen ${screenKey}`}
</Text>
<Text style={style.pushNewScreenLabel} onPress={this.nextScreen}>
Push Screen into this Tab
</Text>
<Text style={style.goBackLabel} onPress={goBack}>
Go back
</Text>
</View>
);
}
}
Run Code Online (Sandbox Code Playgroud)
## TBD 稍微清理一下..
归档时间: |
|
查看次数: |
1177 次 |
最近记录: |