Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use createStackNavigator (React Navigation) in React Native while using TypeScript?

Tags:

I am writing an app using React Native and TypeScript and I use React Navigation for routing.
In the example project I downloaded they use StackNavigator({...}) in order to create the navigator, but when I do npm start I see a warning stating that StackNavigator is deprecated and that I should use createStackNavigator instead. createStackNavigator is also the function I've seen in online tutorials.

The problem is that when using createStackNavigator I get an error:

[ts] Module '".../.../..."' has no exported member 'createStackNavigator'.

I checked and the index.d.ts file in the path indeed doesn't have such a member. I tries npm install @types/react-navigation@latest but that didn't help too.

What can I do so I could use createStackNavigator? Or should I resort to the deprecated StackNavigator?

like image 813
Neo Avatar asked May 24 '18 11:05

Neo


People also ask

How do I use react navigation in typescript?

The stack navigator you will implement in this section will allow the app user to navigate from one screen to another. Start by creating src/ directory that will contain the screen and navigation-related code files. The next step in the example app is to create mock screens. Create a screens/ directory inside src/ .

What is Createstacknavigator in React Native?

Provides a way for your app to transition between screens where each new screen is placed on top of a stack. By default the stack navigator is configured to have the familiar iOS and Android look & feel: new screens slide in from the right on iOS, fade in from the bottom on Android.

Which is better react navigation or React Native navigation?

Integration with existing apps In such a case, any navigator based on JS works well compared to existing native solutions. This gives React Navigation the edge over RNN, especially if you are looking to power a few modules of your existing app with React Native, just like Facebook and many other brownfield apps do.

What is navigator typescript?

Interface NavigatorThe state and the identity of the user agent. It allows scripts to query it and to register themselves to carry on some activities.


1 Answers

The type definition files from @types is not up to date, and will probably not be updated anymore since TypeScript support will be added to the react-navigation library soon.

You can find more information on this here and also an up-to-date version of the type definition file (scroll almost all the way to the bottom). Just copy / paste that snippet over to the index.d.ts file in your node_modules/@types/react-navigation/ folder. This will allow you to use createStackNavigator and others.

like image 59
dentemm Avatar answered Sep 28 '22 07:09

dentemm