Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 3, 2020 03:42 am GMT

Alias in React Native

Creating Alias in React Native:

Is there anything more frustrating to find this path ../../../../../components/location in Javascript?

What if we can use /components/location from any where in the code base?

Enter the Alias

So we are writing alias in .babelrc and we use a babel plugin to get aliasing set up, since we cant use webpack in react native.

Initially, we want to install babel-plugin-module-resolver with yarn or npm.

$ npm install babel-plugin-module-resolver

Once weve installed, open up the projects and find .babelrc file, If there is no .babelrc file then create the file .babelrc in root. Under the plugins key, add the below snippet:-

{  "presets": ["react-native"],  "plugins": [    [      "module-resolver",      {        "root": [          "./src"        ],      "alias": {         "src": "./src",         "assets": "./src/assets"       }      }     ]   ]}

Now we can use src/pages/homepage instead of using complex relative paths like ../../../src/pages/homepage

Now, clear the cache and restart the node server by following steps:

$ npm start --reset-cache$ npm run ios

Thanks for reading this article

I hope you would find the article useful. Feel free to ping me in comment section or @suprabha11


Original Link: https://dev.to/suprabhasupi/alias-in-react-native-1oej

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To