REECT Native:为什么我的屏幕使用堆栈Navigator时可以传递我的屏幕(Props)?

发布于 2025-01-25 19:22:16 字数 1779 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我闹别瞎闹 2025-02-01 19:22:16

首先,在报告中,您无法重新分配const变量。相反,尝试

    let textVal = '';
    if (route.params.title.length > 12) {
        textVal = route.params.title.substring(0,10) + '...';
    }
    else {
        textVal = route.params.title;
    }

const textVal = route.params.title.length > 12
  ? route.params.title.substring(0,10) + '...'
  : route.params.title;

第二,条件中有一个错别字:route.param.title.length应该是route> route.params.title.length。这会丢下错误,因为您无法在未定义的对象上访问属性length

这两个语法错误应防止快速刷新工作,因为结果无法编译。如果您修复了这些问题,并console.log route在报告中的对象,则应获取所需的参数。

First, in ReportMention, you can't reassign a const variable. Instead, try

    let textVal = '';
    if (route.params.title.length > 12) {
        textVal = route.params.title.substring(0,10) + '...';
    }
    else {
        textVal = route.params.title;
    }

Or

const textVal = route.params.title.length > 12
  ? route.params.title.substring(0,10) + '...'
  : route.params.title;

Second, there's a typo in the condition: route.param.title.length should be route.params.title.length. This would throw an error, since you can't access the property length 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文