【问题标题】:React Native navigation reset does not workReact Native 导航重置不起作用
【发布时间】:2021-02-14 15:43:02
【问题描述】:

我的应用程序中有两个堆栈

  • 选项卡导航器(主堆栈)
  • Stack Navigator(应用程序堆栈)

堆栈导航器元素

  • 主屏幕
  • 详细画面
  • 登录屏幕

标签导航器元素

  • 主屏幕(堆栈导航器)
  • 经济新闻
  • 政治新闻
  • 社会新闻

我想从经济新闻、政治新闻和社会新闻转到详细信息屏幕,因为我在这些屏幕中呈现所有新闻,当用户单击任何新的时,必须转到单击的新的详细信息屏幕。我用了 navigate('Home', {screen: 'Detail', params: {id: new.ID}}); 但它总是呈现之前的新细节。

我也试过了

navigation.dispatch(
  CommonActions.reset({
    routes: [
      {
        name: 'Home',
      },
      {
        name: 'Detail',
        params: {
          id: new.ID,
        },
      },
    ],
  }),
);

但它仍然不起作用我需要帮助解决这个问题我尝试了很多东西但总是呈现以前的屏幕细节。

【问题讨论】:

  • 你能发布更多代码吗因为 navigate('Home', {screen: 'Detail', params: {id: new.ID}});应该重置你的参数
  • 当然我会写一个答案我会在那里分享

标签: react-native react-navigation react-native-navigation react-navigation-v5 react-navigation-bottom-tab


【解决方案1】:

这是我的主页堆栈导航

<Stack.Navigator
  initialRouteName="Home"
  headerMode="none"
  transitionConfig={transitionConfig}
>
  <Stack.Screen
    name="Detail"
    component={DetailScreen}
    initialParams={props.route.params}
  />
  <Stack.Screen name="Home" component={HomeScreen} />
  <Stack.Screen name="Profile" component={ProfileScreen} />
  <Stack.Screen name="Login" component={LoginPage} />
  <Stack.Screen name="Settings" component={SettingsScreen} />
</Stack.Navigator>

那是我的主要标签导航

     <Tab.Navigator
        tabBar={props => <Footer {...props} />}
        transitionConfig={transitionConfig}
      >
        <Tab.Screen name="Home" component={StackNavigator} />
        <Tab.Screen name="EconomyNews" component={ContentScreen} />
        <Tab.Screen name="PoliticNews" component={ContentScreen} />
        <Tab.Screen name="SocialNews" component={ContentScreen} />
      </Tab.Navigator>

我想从 EconomyNews、PoliticNews 或 SocialNews 屏幕转到带有新值(如 id)的详细信息屏幕,但它总是呈现前一个屏幕

【讨论】:

  • 一切看起来都很好,添加 DetailScreen 完整代码和 ContentScreen
  • 我无法分享所有代码,因为我分享版主正在删除我的答案,但我可以分享一点。
  • const Detail = React.memo(props =&gt; { const [content, setContent] = useState(null); const dispatch = useDispatch(); const contentProps = useSelector(state =&gt; state.contentReducer); const { loading, error, detail } = contentProps; useEffect(() =&gt; {function fetchData() {dispatch(getDetail({endpoint: URL}));}fetchData();}, []);return (&lt;Container&gt;&lt;Image source={{uri:'URL'}}/&gt;&lt;View style={styles.content_container}&gt;&lt;Title&gt;{content.title}&lt;/Title&gt;&lt;OverView style={styles.content}&gt;{content.overview}&lt;/OverView&gt;&lt;/Container&gt;);});
  • 内容屏幕 > const Content = React.memo(props =&gt; {const { item, navigation } = props; return ( &lt;View&gt; &lt;TouchableWithoutFeedback /n onPress={() =&gt; { navigation.navigate('Home', {screen: 'Detail', params: {id: item.ID}});}}&gt; &lt;Container&gt; &lt;Image source={{uri: 'URL'}}/&gt; &lt;Title&gt;{item.original_title} ({item.title})&lt;/Title&gt; &lt;OverView numberOfLines={5}&gt;{item.overview}&lt;/OverView&gt; &lt;/Container&gt;&lt;/View&gt;); });
【解决方案2】:

很难遵循您的代码,但我没有看到您在 Detail 组件中使用了 id,因此您不可能拥有其他数据。 你的情况不需要备忘录...

const Detail = props => { 
const [content, setContent] = useState(null); 
const dispatch = useDispatch(); 
const contentProps = useSelector(state => state.contentReducer); 
const { loading, error, detail } = contentProps; 
const URL = `URL/${props.navigation?.route?.params?.id}`
useEffect(() => {
function fetchData() {
dispatch(getDetail({endpoint: URL}));}
fetchData();
}, [dispatch, props.navigation?.route?.params?.id]);
return (<Container><Image source={{uri:'URL'}}/><View style={styles.content_container}><Title>{content.title}</Title><OverView style={styles.content}>{content.overview}</OverView></Container>);};

【讨论】:

    【解决方案3】:

    我在 fetch 函数中使用 url 中的 id。我用它从服务中获取数据,这就是为什么我没有写出确切的 url。主要目标是通过重置打开详细信息屏幕。我今天也尝试了另一种方法,但没有奏效;

    import {useNavigation} from '@react-navigation/native';
    const Content = (props => {
       const _nav = useNavigation();
       const goToRoute = () => {
          _nav.reset({
             index: 1,
             routes: [
             {
               name: 'Home',
             },
             {
               name: 'Detail',
               params: {
                 id: item.ID,
               },
             },
          ],
       });
     };
    });
    

    【讨论】:

    • 你可以做点心吗,因为你不需要使用重置。重置参数有问题,我看不到。这是一个准备好反应导航 v5 的模板snack.expo.io/@anthowm/41a9b3
    • snack.expo.io/ye!rF6rv6 这是我类似的零食代码
    • 在这个博览会上,所有工作的你都会得到新鲜的参数。
    • 我已经解决了这个问题 :) 谢谢大家的帮助。我刚刚意识到我的参数保持新鲜但不是 React 状态,我更改了一些关于我的参数的代码。当我得到参数时,我分配了反应状态,我开始在我的 useEffect 函数中使用这些反应状态变量,它可以工作。这都是关于 react 语句和虚拟 dom 的。我不明白这个问题。现在它可以工作了:D:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    相关资源
    最近更新 更多