简体   繁体   English

React Navigation 参数不会重置

[英]React Navigation params doesn't reset

I'm having trouble with resetting the navigation params to null in React Native.我在 React Native 中将导航参数重置为 null 时遇到问题。

MainTab主选项卡
-- Home (stack) -- 主页(堆栈)
-- Misc (stack) -- 杂项(堆栈)
-- Tips (stack) -- 提示(堆栈)

On the Home tab, I have a button to go to Misc, but I want to route to the Tips tab on route to Misc.在“主页”选项卡上,我有一个按钮可以转到“其他”,但我想转到“其他”的“提示”选项卡。
Routing should look like - (Home -> Tips -> Misc)路由应该是这样的 - (Home -> Tips -> Misc)
That button returns the following with params -该按钮返回以下参数 -

this.props.navigation.navigate('Tips', {backRoute: 'Home', routeImGoingNext: 'Misc'});

When these params are passed, I render a back button and a skip button on the Tips screen's navigation based on the backRoute and routeImGoingNext params that were passed from the button on the Home tab.传递这些参数后,我会根据从“主页”选项卡上的按钮传递的 backRoute 和 routeImGoingNext 参数,在“提示”屏幕的导航上呈现后退按钮和跳过按钮。

if(navigation.state.params && navigation.state.params.backRoute){
  return {
    headerLeft: (<HeaderBackButton onPress={()=>navigation.navigate(navigation.state.params.backRoute)}/> ),
    headerRight: (
      <TouchableOpacity onPress={()=>navigation.navigate(navigation.state.params.routeImGoingnext)}>
        <Text style={{paddingRight: 10}}> Skip </Text>
      </TouchableOpacity>
    )
  }
}

My problem occurs when I click the Tips tab after I've already clicked the the button on the Home tab.在单击“主页”选项卡上的按钮后单击“提示”选项卡时,会出现我的问题。 The params are still set and therefore rendering a back button and skip button, but there shouldn't be those buttons if I click the Tips tab.参数仍然设置,因此呈现后退按钮和跳过按钮,但如果我单击“提示”选项卡,就不应该有这些按钮。

Any ideas on how to reset the params when you manually click on the tabs?关于如何在手动单击选项卡时重置参数的任何想法?

I was able to clear the params by manually creating a function and setting the params that are passed to null.我能够通过手动创建一个函数并设置传递给 null 的参数来清除参数。 The clearParams function is called when the header button is pressed.当标题按钮被按下时调用 clearParams 函数。

static navigationOptions = ({navigation}) => {

  clearParams = () => {
    navigation.setParams({backRoute: null, routeImGoingNext: null})
  }

  if(navigation.state.params && navigation.state.params.backRoute){  

    const { backRoute, routeImGoingNext } = navigation.state.params;

    return {
      headerLeft: (<HeaderBackButton onPress={()=>{navigation.navigate(backRoute), clearParams()}}/> ),
      headerRight: (
        <TouchableOpacity onPress={()=>{navigation.navigate(routeImGoingNext), clearParams() }}>
          <Text style={{paddingRight: 10}}> Skip </Text>
        </TouchableOpacity>
      )
    }
  }
 return;
}

react navigation使用它来清除参数

this.props.navigation.setParams({YOUR_PARAMS: null});

I've also run into this issue and I found in documentation that params are shallow merged so if you have some prams from previous navigation and then you navigate to same screen with different params then they will be merged.我也遇到过这个问题,我在文档中发现参数是浅合并的,所以如果你有一些来自以前导航的婴儿车,然后你用不同的参数导航到同一个屏幕,那么它们将被合并。 Here is a link to documentation https://reactnavigation.org/docs/params Quick example will be:这是文档的链接https://reactnavigation.org/docs/params快速示例将是:

navigation.navigate('Receipt', {id: '001', eventId: 'eventIdMock');

and from another screen, if I navigate to Receipt with this params:从另一个屏幕,如果我使用此参数导航到 Receipt:

navigation.navigate('Receipt', {id: '002'});

then the navigation will still have eventId: 'eventIdMock' so the solution is to set explicitly this value to null or undefined那么导航仍然会有eventId: 'eventIdMock'所以解决方案是将此值显式设置为nullundefined

navigation.navigate('Receipt', {id: '002', eventId: null});

在此处输入图片说明

This worked for me, straight from the documentation.这对我有用,直接来自文档。 Note that this resets the entire navigation state (props/routes/params etc...).请注意,这会重置整个导航状态(道具/路线/参数等...)。 In my case this was fine.就我而言,这很好。

https://reactnavigation.org/docs/navigation-prop/ https://reactnavigation.org/docs/navigation-prop/

    props.navigation.reset({
        index: 0,
        routes: [{name: "Screen you need to go back to"}]
    });

My app is a simple 3 screen app in react native using react navigation "bottom tab navigator".我的应用程序是一个简单的 3 屏幕应用程序,使用 react 导航“底部选项卡导航器”进行本机反应。 All of my components are functional components so I did not use any sort of navigation-actions from this section of the docs: https://reactnavigation.org/docs/navigation-actions .我的所有组件都是功能组件,因此我没有使用文档这一部分中的任何类型的导航操作: https : //reactnavigation.org/docs/navigation-actions

See my stack-navigation below.请参阅下面的堆栈导航。 In the above code snippet, index: 0 would be the screen name "SetGame" below.在上面的代码片段中, index: 0 将是下面的屏幕名称“SetGame”。 index: 1 is "PlayGame" and index: 2 is "History".索引:1 是“PlayGame”,索引:2 是“历史”。 In my case I'm navigating back to index 0 which is "SetGame" and reseting routes/params/history in the navigation state object.在我的情况下,我导航回索引 0,即“SetGame”并在导航状态对象中重置路线/参数/历史记录。

    <Tab.Navigator
  initialRouteName="Find"
  tabBarOptions={{
    tabStyle: {
      backgroundColor: '#76B947',
      height: 90,
      paddingBottom: 30,
      margin: 0,
    },
    style: {
      height: 90,
    },
    activeTintColor: '#292929',
    inactiveTintColor: '#ededed',

  }}
>
  <Tab.Screen
    name="SetGame"
    component={SetGame}
    options={{
      tabBarIcon: ({ color }) => (
        <Feather name={'list'} size={25} color={color} />
      ),
    }}
  />
  <Tab.Screen
    name="PlayGame"
    component={PlayGame}
    options={{
      tabBarIcon: ({ color }) => (
        <Feather name={'play'} size={25} color={color} />
      ),
    }}
  />
  <Tab.Screen
    name="History"
    component={GameHistory}
    options={{
      tabBarIcon: ({ color }) => (
        <Feather name={'save'} size={25} color={color} />
      ),
    }}
  />
</Tab.Navigator>

In my app inside of the PlayGame component there is a function to goBackToSetGame.在我的 PlayGame 组件内的应用程序中,有一个 goBackToSetGame 函数。 Meaning we want to reset our game which clears data in the database and resets navigation state.这意味着我们要重置我们的游戏,清除数据库中的数据并重置导航状态。 Essentially disabling the user to play a game until it gets set again.本质上是禁止用户玩游戏,直到再次设置为止。

    const goBackToSetGame = async () => {
    const gameInputs = {
      email: user.profile.email,
      players: [],
      holes: [],
      names: [],
      createdAt: Date.now(),
      gameStatus: "not-playing",
      scores: [{}]
    }

    await API.QuitGame(gameInputs).then((res) => {
      // reset scores state if the game is quit
      setScores(null);
      console.log("Changed gameStatus from server response: ", 
    res.data)
    })
    .catch(err => console.log(err));

    // reset props.navigation so you have to 
    // start a new game before coming back to this screen
    props.navigation.reset({
      index: 0,
      routes: [{name: "SetGame"}]
    });
 }

You try to check this post , will solve your idea 您尝试检查此帖子,将解决您的想法

https://reactnavigation.org/docs/en/stack-actions.html https://reactnavigation.org/docs/en/stack-actions.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM