我正在開發一個舊的 React Native 應用程式并且不熟悉基于類的組件,我有一個布林值 (isJobOwner),我想將它從一個螢屏傳遞到另一個螢屏,然后在下一個螢屏上檢查 isJobOwner 布林值是真還是假,在這種情況下,我將根據使用三元運算子的真值或假值將用戶帶到最終螢屏。
我的 2 個檔案目前看起來像這樣:
注冊選擇.js:
class SignUpChoice extends React.Component {
static navigationOptions = ({ navigation }) => ({
header: (
<Header
leftComponent={<GoBackComponent onPress={() => navigation.goBack()} />}
containerStyle={commonStyles.headerContainer}
centerComponent={<CustomHeader name="Account Type" />}
/>
),
});
onChooseRole(isJobOwner) {
saveRole(isJobOwner ? 'owner' : 'seeker');
NavigationService.navigate('Tutorial', { isJobOwner: isJobOwner });
}
render() {
return (
<Container>
<Content>
<View style={styles.signUpChoiceContainer}>
<Button
style={styles.jobButton}
onPress={() => this.onChooseRole(true)}
>
<Text style={styles.jobText}>
Are you looking for a Tradesman?
</Text>
</Button>
<Button
style={styles.jobButton}
onPress={() => this.onChooseRole(false)}
>
<Text style={styles.jobText}>Are you looking for work?</Text>
</Button>
</View>
</Content>
</Container>
);
}
}
教程.js:
class Tutorial extends React.Component {
static navigationOptions = { header: null };
constructor(props) {
super(props);
this.state = {
isLoading: false,
};
}
onNextStep() {
// NavigationService.navigate(isJobOwner ? 'FindEmployers' : 'FindJobs');
this.props.navigation.navigate(
this.props.isJobOwner ? 'FindEmployers' : 'FindJobs'
);
}
onGoBack() {
this.props.navigation.navigate('SignUpChoice');
}
render() {
return (
<Container>
{(this.props.requesting || this.state.isLoading) && (
<Loader size={'large'} color={colors.white} />
)}
<Content contentContainerStyle={{ height: '100%' }}>
<View style={styles.buttonsContainer}>
<Button
transparent
style={styles.tutorialButton}
onPress={() => this.onNextStep()}
>
<Text style={styles.tutorialButtonText}>Next Step</Text>
</Button>
<Button
transparent
style={styles.backButton}
onPress={() => this.onGoBack()}
>
<Text style={styles.backButtonText}>Go Back</Text>
</Button>
</View>
</Content>
</Container>
);
}
}
單擊 onNextStep 時,我想導航到 FindEmployers 或 FindJobs 螢屏,具體取決于 isJobOwner 是真還是假。
我知道這很簡單,但任何快速的建議將不勝感激!非常感謝。
uj5u.com熱心網友回復:
如果您使用的是reactnavigation,您可以:
this.props.navigation.navigate('Tutorial', { isJobOwner: isJobOwner });
然后使用以下命令檢索路由引數:
class Tutorial extends React.Component {
render() {
const { navigation } = this.props;
const isJobOwner = navigation.getParam('isJobOwner', false);
...
}
}
查看檔案https://reactnavigation.org/docs/3.x/params了解更多
uj5u.com熱心網友回復:
你的代碼是錯誤的。
this.props.isJobOwner
這是正確的代碼。
this.props.route.params.isJobOwner
請測驗使用 console.info(this.props.route.params)
總之,
第一個螢屏: this.props.navigation.navigate('Tutorial', { isJobOwner: isJobOwner })
第二個螢屏: console.info(this.props.route.params.isJobOwner
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/327711.html
