English 中文(简体)
如何使用导航。 取代原样?
原标题:how to use navigation.replace in TypeScript?

我试图在我的法典中利用这一点:

const navigation = useNavigation();
navigation.replace( AllFriends )

但我仍犯了一个错误:

Property  replace  does not exist on type  NavigationProp<Record<string, object | undefined>, string, NavigationState, {}, {}> .

我也试图利用这一方法。

const navigation =  useNavigation<StackNavigationProp<{route: {} }>>()
navigation.replace( route );

如下文所示:https://github.com/react-navigation/react-navigation/issues/82<56/a>。

but this gives me an error on replace that expected 2 arguments but got only 1...

I am using "@react-navigation/native": "^5.5.1",

问题回答

The replace method is only available to screens within StackNavigators. We need a way to explicitly type that the screen is such.

当你在StackNavigator时,你有能力通过界定你的那种方式。 路线及其预期得到的参数:

export type StackParams = {
   MyRoute: undefined;
   AnotherRoute: {
      id: number
   }
}

createStackNavigator<StackParams>();

Then in the screen you pass that param list to StackNavigationProp

import { StackNavigationProp } from  @react-navigation/stack/ ;
import { StackParams} form  your/routes ;

const navigation =  useNavigation<StackNavigationProp<StackParams>>()
navigation.replace( route );

以此申明,这是一种圣卡片检查,并提供了你可以通航的路线清单:

replace( AnotherRoute ) //success!
replace( FakeRoute ) //error, this was not defined in our RouteParams
const navigation =  useNavigation<StackNavigationProp<{route: {} }>>()
navigation.replace( route );

日复一日,仍在使用班级部件,并设有标准的纳格托帕拉姆派

export type NavigatorParamList = {
 ...
}

export type ScreenProps = NavigationProp<NavigatorParamList>

export type ScreenComponentProps<T extends keyof NavigatorParamList> = {
  navigation: NativeStackNavigationProp<NavigatorParamList, T> // changed from navigation: NavigationProp<NavigatorParamList, T>
  route: RouteProp<NavigatorParamList, T>
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签