[英]React-native: Binding scene to NavigationBar
我正在整理這個Ask / Answer應用程序,並遇到了這個障礙:
我想從navigationBar觸發場景中的一個函數。 與登錄應用程序類似,我在NavigationBar中有一個按鈕來提交答案:
RightButton(route, navigator, index, navState) {
if (route.name=='TextInputView'){
return <TouchableHighlight
underlayColor="transparent"
style={{ marginRight:13, marginTop:0}}
onPress={() => this.refs.TextInputView.submitSolution()}>
<Text style={ styles.leftNavButtonText }>Done</Text>
</TouchableHighlight>
}
renderscene(route,navigator)看起來像這樣:
if (route.name == 'TextInputView'){
return <TextInputView ref="TextInputView" navigator={navigator} {...route.passProps}/>
}
當然在“TextInputView”組件中我有一個“submitSolution”函數......
問題是如果我在場景中並按下“完成” - 我總是得到:“undefined不是一個對象(評估'_this2.refs.TextInputView')
一如既往:感謝您的幫助
好吧,你可以通過在場景中保留與提交相關的所有內容來實現。
怎么樣?
您可以通過按需在導航欄中注入按鈕來完成此操作。
這是我為我正在開發的項目創建的解決方案: https : //rnplay.org/apps/dS31zw
我們的想法是為navbar路由提供一個具有標簽和功能的對象(以及任何你喜歡的東西......例如圖標。)
作為獎勵,你也可以注入標題!
注意:這個技巧取決於在NavigationBarRouteMapper工作之前調用的componentWillMount()。 如果將來這種變化,它肯定會打破它。 但它現在完美無瑕!
'use strict';
import React, {Component} from 'react';
import ReactNative from 'react-native';
const {
AppRegistry,
StyleSheet,
Text,
View,
Navigator,
Alert,
TouchableHighlight
} = ReactNative;
class Home extends Component {
//This trick depends on that componentWillMount fires before the navbar is created
componentWillMount() {
this.props.route.navbarTitle = "Home";
this.props.route.rightNavButton = {
text: "Button",
onPress: this._doSomething.bind(this)
};
}
_doSomething() {
Alert.alert(
'Awesome, eh?',
null,
[
{text: 'Indeed'},
]
)
}
render() {
return (
<View style={styles.container}>
<Text>You are home</Text>
</View>
);
}
}
class AppContainer extends Component {
renderScene(route, navigator) {
switch(route.name) {
case "Home":
//You must pass route as a prop for this trick to work properly
return <Home route={route} navigator={navigator} {...route.passProps} />
default:
return (
<Text route={route}
style={styles.container}>
Your route name is probably incorrect {JSON.stringify(route)}
</Text>
);
}
}
render() {
return (
<Navigator
navigationBar={
<Navigator.NavigationBar
style={ styles.navbar }
routeMapper={ NavigationBarRouteMapper } />
}
initialRoute={{ name: 'Home' }}
renderScene={ this.renderScene }
/>
);
}
}
//Nothing fancy here, except for checking for injected buttons.
var NavigationBarRouteMapper = {
LeftButton(route, navigator, index, navState) {
if(route.leftNavButton) {
return (
<TouchableHighlight
style={styles.leftNavButton}
underlayColor="transparent"
onPress={route.leftNavButton.onPress}>
<Text style={styles.navbarButtonText}>{route.leftNavButton.text}</Text>
</TouchableHighlight>
);
}
else if(route.enableBackButton) {
return (
<TouchableHighlight
style={styles.leftNavButton}
underlayColor="transparent"
onPress={() => navigator.pop() }>
<Text style={styles.navbarButtonText}>Back</Text>
</TouchableHighlight>
);
}
},
RightButton(route, navigator, index, navState) {
if(route.rightNavButton) {
return (
<TouchableHighlight
style={styles.rightNavButton}
underlayColor="transparent"
onPress={route.rightNavButton.onPress}>
<Text style={styles.navbarButtonText}>{route.rightNavButton.text}</Text>
</TouchableHighlight>
);
}
},
Title(route, navigator, index, navState) {
return (<Text style={styles.navbarTitle}>{route.navbarTitle || route.name}</Text>);
}
};
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
marginTop: 66
},
navbar: {
backgroundColor: '#ffffff',
},
navbarTitle: {
marginVertical: 10,
fontSize: 17
},
leftNavButton: {
marginVertical: 10,
paddingLeft: 8,
},
rightNavButton: {
marginVertical: 10,
paddingRight: 8,
},
navbarButtonText: {
fontSize: 17,
color: "#007AFF"
}
});
AppRegistry.registerComponent('AppContainer', () => AppContainer);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.