Bear with me, I'm not sure if this is purely a React Native issue, or just an ES6 question in general. But I noticed I'm unable to do this:
import {navBarRouteMapper} from '/src/helpers';
I get an error saying it's unable to resolve the module. I have to do this instead:
import {navBarRouteMapper} from '../../../src/helpers';
Keeping track of folder depth can get a bit unmanageable as the complexity of the app grows. Why am I not able to use an absolute path?
EDIT:
I see people are recommending adding babel, but I don't want to pollute React Native's system. There's obviously transpilation to ES6 already going on. I was hoping for a solution specific to the React Native ecosystem.
Absolute import in React Typescript json file inside project root directory. Just open it and add the same baseUrl setting as Javascript. Happy Learning!
An absolute path is defined as the specifying the location of a file or directory from the root directory(/). In other words we can say absolute path is a complete path from start of actual filesystem from / directory.
Relative links show the path to the file or refer to the file itself. A relative URL is useful within a site to transfer a user from point to point within the same domain. Absolute links are good when you want to send the user to a page that is outside of your server.
An absolute path is also known as the full path and starts with / in Linux and macOS and C:/ on Windows. To find an absolute path in Python you import the os module then you can find the current working directory using os. path. abspath("insert-file-name-here") in your Python script.
There is actually a pretty clean solution for React Native, have a look here: https://medium.com/@davidjwoody/how-to-use-absolute-paths-in-react-native-6b06ae3f65d1#.u47sl3p8x.
TL;DR:
You'll just have to create a package.json
file in your src/helpers
folder:
{
"name": "@helpers"
}
And you will be able to import it from anywhere:
import { navBarRouteMapper } from '@helpers'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With