reactnative package.json dependencies updation - react-native

I am creating a react-native application. Here I am creating one library on npm as well. And using this library from my react application inside package.json.
Now, I want to automate one task here. If I update the code and version of my library on npm, my react-native application should adopt the new version automatically.
How could I achieve this task?
Any ideas regarding same?
Thanks in advance.

If your library is on any remote server and you have added it into your project using package manager (like yarn).
Now you have updated your library but it will not update automatically in your react native project.
You have to achieve this manually by specifying the library version.
yarn add package-name#1.2.3
Refer: package using yarn

Related

How to use expo module in react-native app?

I have existing react-native project and I want to use expo-in-app-purchases module, and can't find clear doc how to do that. unimodules from its README are deprecated, and not sure that migration is the right one.
I want only one module, without migrating to expo.
In order to use 'expo-in-app-purchases' you have to install Expo modules, as stated in docs:
You must ensure that you have installed and configured Expo modules before continuing.
It doesn't mean that you have to migrate the whole project to expo. There are two scenarios:
You don't have Expo modules in your project: use automatic installation,
You have react-native-unimodules installed: migrate to Expo modules using this guide

is it necessary to rebuild react-native project after linking

I would like to know if it's necessary to rebuild react native project after installing and linking libraries that includes native codes.
Yes, as react-native-link updates some native files that are under the ios/android folder, that won't be checked during a reload/hot-reload/live-reload. All those files aren't checked in your jsBundle and are compiled during a run-android or run-ios.
Of course, if you added a new dependecy to the project and linked it, and you haven't used it anywhere in your code yet, you don't need it. If you are using that dependecy, your project instance will, most likely, die.
if your react native version is below 0.60 then you have to run react-native-link after you install any react-native module. However after react-native version 0.60 and above, this is done automatically using the new "autolinking" feature added.
Read the changelog here:
https://facebook.github.io/react-native/blog/2019/07/03/version-60
However you still need to rebuild your project after you install a native module but you dont need to run the react-native-link command anymore after installing every library after react-native#0.60 and above. Just type react-native run-android

When to use react-native link?

I understand that react-native link (see post) is an automatic way to install native dependencies. The post above explain how to use the link command but lacking the detail of when to use it. Should it be used after adding a component, every code change or after introducing new module to the app?
Why we use react-native-link??
In this post, I will explain why we use react native link command and when we need to use this command or not?
First, we will understand what is native module??
Native Modules
Native modules are usually distributed as npm packages, apart from the typical javascript files and resources they will contain an Android and ios library project. 
React Native provides an impressive amount of native modules that give you direct access to core platform APIs on both Android and IOS.
For example react-native-maps, react-native-firebase, react-native-socketio etc.
These modules or packages contained both platform (Android and Ios) code.
Now coming on to the react native link
Those libraries which are use native code, in that case you'll have to add these files to your app. For linking those library with react native code need to run react-native-link
Here are the few steps to link your libraries that contain native code
Automatic linking
Step 1
Install a library with native dependencies:
$ npm install --save
Step 2
Link your native dependencies:
$ react-native link
Done! All libraries with native dependencies should be successfully linked to your iOS/Android project.
Where we don't use react-native-link??
Those components which are only written in javascript they are not using any native code (Android and Ios). There is no need to run react-native-link.
For example rn-modal-picker, react-native-check-box etc.
You should check out this other answer: The use of react-native-link command?
You only need to run react-native link NAME_OF_PACKAGE when you install a new package that has a native codebase, or without arguments if you want to do it for multiple of them.

What is react-native link?

What is the purpose of the react-native link command?
Note: from React-Native 0.60.0 linking packages using react-native link has become redundant. Autolink has been added to the React-Native CLI which means that iOS will now use cocoapods and Android will use gradle. You can read more about Autolinking here.
What is react-native link?
react-native link is an automatic way for installing native dependencies. It is an alternative to manually linking the dependency in your project. It works for both Android and iOS.
When linking a project manually the majority of the steps are the same and so using react-native link allows you to install the native dependency with less fuss and without having to type similar code or perform similar actions repeatedly.
However, it should be noted that running react-native link will not always link a package fully, sometimes additional steps are required and you should check carefully with the installation instructions.
Always read the instructions carefully before installing a dependency and linking it.
iOS Considerations
If your project is using CocoaPods and the dependency that you are linking has a .podspec then when you use run react-native link it will update your Podfile. This is instead of adding the files directly to your Xcode project. You will also have to run pod install inside your ios directory otherwise the native dependency won't be fully installed.
Sometimes installing using CocoaPods can cause more issues, and not every dependency needs to be installed with CocoaPods you could always follow the steps that I outlined in this SO answer to stop react-native link adding a dependency to the Podfile, it is not ideal but it is a workaround. Some dependencies require additions to be made to the Podfile, so you should only do this if the dependency doesn't require pods to run.
react-native link or react-native link dependency-name
Should you just use react-native link when linking any dependency or should you be more explicit and use react-native link dependency-name?
From my experience it is better to use react-native link dependency-name. This is due to the fact that react-native link will try to link (or re-link) all the dependencies that can be linked and this can lead to code duplication. Most of the issues that I have experienced have been when the Android native dependency is being linked. I think there has been some headway in stopping this from happening in subsequent updates, but the old adage applies here once bitten, twice shy
Linking good practice
When using react-native link dependency-name you should follow good practice so that you don't get stung. Sometimes dependencies that you try don't work as expected and removing all the code that was added during the linking process can be tricky. (Xcode project files can be a nightmare to go through if you are unfamiliar with them).
This is how I install dependencies and then link them.
Make sure that you are using version control, like git.
Make sure your code is fully committed with no unsaved changes.
Create a new branch, and check it out.
Install your dependency npm i dependency-name
Then link you dependency react-native link dependency-name
Perform any additional installation steps that the dependency requires. See the installation instructions for the dependency.
Check that your code works with the new dependency.
commmit changes and merge the branch.
Manual Linking
If you prefer to link your native dependencies manually then you should either follow the instructions on the dependency's website or you can look at the documentation that react-native provides.
Currently there is only an explanation on how to manually link iOS projects.
Manually linking Android requires you to make changes in the following locations:
settings.gradle
app/build.gradle
MainApplication.java
As always for the exact changes that you should make you should look at the dependency's manual linking instructions.
Do I have to link?
It depends on the dependency that you are using some dependencies use only code written in Javascript, so it is not required to link them, and there is no benefit served by running react-native link dependency-name.
However, if the dependency contains native code then you will have to link. Either manually or by using react-native link dependency-name.
How can I tell if I need to link the dependency?
Firstly you need to check the website, the github repo, or the npmjs.com page for the dependency. There will usually be instructions there telling you whether to link the dependency after you have installed it.
If you cannot find any instructions about linking, you (probably) won't need to link it.
If you are still unsure, check with the dependency maintainer.
Can I just run link anyway?
Yes, you can it won't do anything if there is nothing to link. But always run it with react-native link dependency-name to avoid issues.
When do I run link?
You only run it after you have installed your dependency. I would recommend running it just after you have installed the dependency. You should then check to make sure that it works, before installing any new dependencies so that you can easily debug.
You shouldn't need to run it more than once per dependency.
It doesn't matter how many components or changes to the javascript code that you make it won't affect the linking, as the linking is purely native and components are javascript.
What is autolink?
Autolink is a new feature that is being added to the react-native-cli. You can read more about autolink here.
Autolink replaces react-native-link
Autolinking is a mechanism built into CLI that allows adding a
dependency with native components for React Native to be as simple as:
yarn add react-native-webview
Autolinking is a replacement for react-native link that brings new features (such as ability to easily integrate native dependencies on iOS) and fixes some of the long-standing issues.
Once it is fully implemented it should make adding dependencies with native-code to your project much easier.
Linking native library means that you are going to integrate already implemented module into your application or module which completes your react native's module functionality.
Steps to integrate lib(android):
1) Add package name to new packages() 2) Add dependencies to settings.gradle file and main application's gradle i.e app/gradle file. 3) sync the projects gradle because you made changes in the gradle and it's done.
All manual steps like below you have to perform
1) Go to your project's home dir using cmd.
2) run npm install
3) Thereafter run rnpm link or react-native link
4) see ios folder in your project folder and if you find any pod file then run pod install after navigating into ios folder in cmd.
now instead,
1) Go to your project's home dir using cmd.
2) run npm install
3) Thereafter run rnpm link or react-native link
4) see ios folder in your project folder and if you find any pod file then run pod install after navigating into ios folder in cmd.
When you've installed a library with native content(android or ios) then you need to require linking to the component react-native library.
like this
react-native link react-native-sound-player
When you installed any third party library with native content,you have to link the dependencies in android and ios. react-native link is responsible for including the dependencies in gradlew and pod files.
for suppose you installed react-native-vector-icons package then you have to link in your gradlew files. the new version of react native 0.60 has auto linking.
npm install react-native-vector-icons
react-native link react-native-vector-icons
Thanks for asking this question
if you are working with fonts
react-native link is used for linking the fonts and assets to project
link command is also for linking the libraries to react-native
But now React-native too better so there is no need to link the library
we can use
pos install
npx jetify
it will automatically link the libraries
react-native link is an automatic way for installing native dependencies. It is an alternative to manually linking the dependency in your project. It works for both Android and iOS

Which is the correct method of upgrading React Native in your app?

In React Native, what is the difference between:
react-native-git-upgrade
react-native upgrade
yarn upgrade react-native
Just deleting node_modules, bumping the version number in package.json, removing yarn.lock or package-lock.json and doing an npm install or yarn install
What is the preferred method, advantages/disadvantages? Are some outdated? Only relevant for certain versions?
I want to love React Native, but when it comes to versions, it is absolute hell. 0.44.1 is the only version that has consistently worked for me without random fatal errors on startup.
As you can see in here in the official react-native document, the best way for upgrading react-native is using react-native-git-upgrade if you create your project with react-native init. If you started you app with create-react-native-app please read here.
Because React Native projects built with native code are essentially made up of an Android project, an iOS project, and a JavaScript project, upgrading can be rather tricky.
Advantage: The module react-native-git-upgrade provides a one-step operation to upgrade the source files with a minimum of conflicts. Under the hood, it consists in 2 phases:
First, it computes a Git patch between both old and new template
files,
Then, the patch is applied on the user's sources.