What does react-native link
do?
https://github.com/react-native-community/react-native-svg doc says to do
react-native link react-native-svg
is it the same as
pod 'RNSVG', :path => '../node_modules/react-native-svg'
# pod install
CocoaPods allows you to add code to run before and at the end of the Podfile via installation hooks. You probably already have some custom code in there. This plugin allows all that code to be centralized and reasoned about in a single place per React Native version.
Cocoapods is the only way to add React Native and its' native modules to iOS projects. Cocoapods uses a configuration file called a Podfile to instruct the Cocoapods dependency manager which modules to add to your Xcode project.
CocoaPods is a dependency management system for iOS (and other Cocoa-based) projects. It is very similar in function and usage to npm for JavaScript and Bundler for Ruby.
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.
Cocoapods is the dependency manager for iOS. Just like npm is for JavaScript (more specifically Node.js) projects.
Let's switch the example to use react-native-device-info
.
The project asks for you to create or add the following to a Podfile
(if you're linking manually).
pod 'RNDeviceInfo', :path => '../node_modules/react-native-device-info'
This will add RNDeviceInfo
as an iOS dependency and will look inside of '../node_modules/react-native-device-info'
to get it. More specifically it'll look for the .podspec file.
If you take a look at the .podspec file, you'll see something like
s.source_files = "RNDeviceInfo/*.{h,m}"
. All the file is going to do is grab the matching sources (RNDeviceInfo.h
and RNDeviceInfo.m
) and store them inside of RNDeviceInfo
directory. I think (total guess), the directory name matches s.name
.
react-native link ...
effectively does the same thing, automatically. I quote from React docs:
Link your native dependencies: react-native link Done! All libraries with native dependencies should be successfully linked to your iOS/Android project.
Both accomplish the same result, using react-native link
will automate the linking of native libraries.
Just like this answer said, react-native link
add your dependencies in cocoapods
Podfile
for you automatically, if your ios project used cocoapods
.
But it is not like this before react-native v0.50.0
. It wrote dependencies' info ( file reference
, header search path
, library
) into your project's xcodeproj
directly. You still can see the old code in react-native v0.57.
So, why uses cocoapods
?
I guess it's for consistent package management. Without cocoapods
, if your dependency, named A, is dependent on another popular module B from iOS communtity , the author of A would need to write some script like install-B.sh
to download B's files, or even worse add B into A project directly.
See ios-install-third-party.sh
in react-native v0.48 for example.
Also, your iOS development colleagues would like to use cocoapods
(rather than xcodeproj
). It is important to use the same package manager for javascript developers and iOS developers. Both you know all native modules that your project includes.
react-native link
with cocoapods
can let we leverage the two communities.
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