javascript - 在页面之间发送值反应原生钩子
问题描述
我是 React Native 的新手,坚持将数据传递到另一个页面。在这种情况下,我想将数据从 QR 阅读器发送到另一个页面。
这是我在第一个屏幕上的代码:
const LoginScreen = (props) => {
const onSuccess = e => {
const Data = e.data
console.log(Data);
props.navigation.replace("Input Water Transaction", {result: Data })
};
return (
<QRCodeScanner
onRead={onSuccess}
flashMode={RNCamera.Constants.FlashMode.off}
topContent={
<Text>
Scan QR Water
</Text>
}
bottomContent={
<TouchableOpacity style={styles.buttonTouchable}>
<Text style={styles.buttonText}>Scan QR Water</Text>
</TouchableOpacity>
}
/>
);
}
这是第二个屏幕:
const InputWater = (props) => {
const navigation = useNavigation();
const QRResult = props.navigation.getParam('result', 'nothing sent')
return (
<ScrollView>
<View style={{
flex:1,
}}>
<Root>
<TouchableOpacity
onPress={() =>
Popup.show({
type: 'Success',
title: 'Upload complete',
button: true,
textBody: 'Congrats! Your upload successfully done',
buttontext: 'Ok',
callback: () => navigation.navigate('Scan QR Water')
})
}
>
<View style={{
backgroundColor: '#4cd137',
marginTop: 10,
borderRadius: 10,
}}>
<Text style={{
marginTop: 10,
marginBottom: 10,
fontWeight: 'bold',
textAlign: 'center'
}}>{QRResult}</Text>
</View>
</TouchableOpacity>
</Root>
<View style={{
flexDirection: 'column',
justifyContent: 'flex-start',
alignItems: 'center',
alignContent: 'center',
marginTop: 10,
}}>
<View style={{
width: 350,
minWidth: 310,
maxWidth: 310,
backgroundColor: '#d6e0f0',
marginTop: 10,
borderRadius: 10,
alignItems: 'stretch',
}}>
<View style={{
backgroundColor: '#fff',
marginTop: 5,
marginLeft: 5,
marginRight: 5,
borderRadius: 10,
marginBottom: 5
}}>
<View style={{
marginTop: 10,
marginBottom: 10,
marginLeft: 10,
flexDirection: 'column'
}}>
<Text>Electricity Meter No</Text>
<TextInput style={{
backgroundColor: '#ecf0f1',
borderRadius: 10,
height: 35,
minWidth: 240,
maxWidth: 280,
marginTop: 5,
}}>
</TextInput>
<Text style={{
marginTop: 10
}}>Start Electricity Meter</Text>
<TextInput style={{
backgroundColor: '#ecf0f1',
borderRadius: 10,
height: 35,
minWidth: 240,
maxWidth: 280,
marginTop: 5,
}}>
</TextInput>
<Text style={{
marginTop: 10
}}>Last Electricity Meter</Text>
<TextInput style={{
backgroundColor: '#ecf0f1',
borderRadius: 10,
height: 35,
minWidth: 240,
maxWidth: 280,
marginTop: 5,
}}>
</TextInput>
<Text style={{
marginTop: 10
}}>Lost Electricity(%)</Text>
<TextInput style={{
backgroundColor: '#ecf0f1',
borderRadius: 10,
height: 35,
minWidth: 240,
maxWidth: 280,
marginTop: 5,
}}>
</TextInput>
</View>
</View>
</View>
</View>
<View style={{
justifyContent: 'center',
flexDirection: 'column',
marginTop: 200
}}>
<Text style={{
fontSize: 20,
fontWeight: 'bold',
textAlign: 'center'
}}>Water Transaction</Text>
</View>
</View>
</ScrollView>
);
};
但我收到这样的错误:
ERROR TypeError: props.navigation.getParam is not a function. (In 'props.navigation.getParam('result', 'nothing sent')', 'props.navigation.getParam' is undefined)
如何从第一个屏幕获取值?或者我做错了?我很感激任何建议,谢谢!
解决方案
如果您使用的是反应导航 5
props.route.params
如果年龄较大
props.navigation.state.params
它在使用 navigation.navigate('ScreenName', {params}) 导航时有效
我不确定navigation.replace
推荐阅读
- c# - 如何使用类型变量使用 Dapper 进行查询
- netlogo - 对链接、self 和 end2 语法的困惑
- javascript - 有没有办法限制使用 sequelize select 查询返回的小数位数?
- java - 哪个更好地使用带有单例对象的函数或带有类的静态函数
- mysql - Mysql Kubernetes Deployment helm chart 失败,准备就绪,liveness probe failed
- wordpress - 在 Wordpress 循环中显示帖子的类别
- database - 如何将数据模型转换为全新的数据模型?
- python - 根据行中的轴计算斜率
- c# - 在 Serenity .NET 5 中访问静态 ISqlConnections
- vba - 每 15 分钟复制一行