REECT Native:为什么我的屏幕使用堆栈Navigator时可以传递我的屏幕(Props)?
app.js
const Stack = createNativeStackNavigator();
export default function App() {
return (
<Provider store={store}>
<NavigationContainer>
<Stack.Navigator initialRouteName="Main">
<Stack.Screen options={{headerShown: false}} name="Main" component={MainScreen} />
<Stack.Screen options={{headerShown: false}} name="Report" component={ReportScreen} />
</Stack.Navigator>
</NavigationContainer>
</Provider>
);
}
./screens/reportscreen.js
import React from 'react';
import {
StyleSheet,
SafeAreaView,
View,
Text,
} from 'react-native';
import ReportReason from '../components/ReportReason';
const ReportScreen = ({route}) => {
const textVal = '';
if (route.param.title.length > 12){
textVal = route.params.title.substring(0,10) + '...';
}
else {
textVal = route.params.title;
}
return(
<SafeAreaView style = {reportStyles.container}>
<View style = {reportStyles.card}>
<Text>Report</Text>
<Text>{textval}</Text>
<ReportReason/>
</View>
</SafeAreaView>
);
};
的一部分
const ReportMension = ({mension}) => {
return(
TouchableOpacity style={postModalStyles.listButton} onPress={()=>{
console.log(mension)
navigation.navigate('Report', { title: mension.title });
}}>
<Text style={postModalStyles.text}>report</Text>
</TouchableOpacity>
);
}
。
Console.Log正常操作。 为什么我的组件和导航器无法传递参数(props)? 使用堆栈Navigaor时如何通过道具?
App.js
const Stack = createNativeStackNavigator();
export default function App() {
return (
<Provider store={store}>
<NavigationContainer>
<Stack.Navigator initialRouteName="Main">
<Stack.Screen options={{headerShown: false}} name="Main" component={MainScreen} />
<Stack.Screen options={{headerShown: false}} name="Report" component={ReportScreen} />
</Stack.Navigator>
</NavigationContainer>
</Provider>
);
}
./screens/ReportScreen.js
import React from 'react';
import {
StyleSheet,
SafeAreaView,
View,
Text,
} from 'react-native';
import ReportReason from '../components/ReportReason';
const ReportScreen = ({route}) => {
const textVal = '';
if (route.param.title.length > 12){
textVal = route.params.title.substring(0,10) + '...';
}
else {
textVal = route.params.title;
}
return(
<SafeAreaView style = {reportStyles.container}>
<View style = {reportStyles.card}>
<Text>Report</Text>
<Text>{textval}</Text>
<ReportReason/>
</View>
</SafeAreaView>
);
};
part of ./components/ReportMension.js
const ReportMension = ({mension}) => {
return(
TouchableOpacity style={postModalStyles.listButton} onPress={()=>{
console.log(mension)
navigation.navigate('Report', { title: mension.title });
}}>
<Text style={postModalStyles.text}>report</Text>
</TouchableOpacity>
);
}
this is component of screen named Main.
console.log is operated normally.
why my component and navigator are cannot passed params(props)?
how to pass props when using stack navigaor??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,在报告中,您无法重新分配
const
变量。相反,尝试或
第二,条件中有一个错别字:
route.param.title.length
应该是route> route.params.title.length
。这会丢下错误,因为您无法在未定义的对象上访问属性length
。这两个语法错误应防止快速刷新工作,因为结果无法编译。如果您修复了这些问题,并console.log
route
在报告中的对象,则应获取所需的参数。First, in ReportMention, you can't reassign a
const
variable. Instead, tryOr
Second, there's a typo in the condition:
route.param.title.length
should beroute.params.title.length
. This would throw an error, since you can't access the propertylength
on an object that is undefined.These two syntax errors should prevent fast refresh from working, since the result wouldn't compile. If you fix these, and console.log the
route
object in ReportMention, you should get the params you're looking for.