React Native 填坑之路

以下是我在開發過程中遇到的一些問題以及解決方法總結:

歡迎大家加入RN討論群,一起填坑:670055368

1、升級Xcode11, React Native啟動報錯的問題!

Exception '*** -[__NSArrayM objectAtIndexedSubscript:]: index 1 beyond bounds [0 .. 0]' was thrown while invoking getCurrentAppState on target AppState with params
[RCTModuleMethod.mm:376] Unknown argument type '__attribute__' in method -[RCTLinkingManager getInitialURL:reject:]. Extend RCTConvert to support this type.
image.png

解決辦法:
找到這個文件


image.png

并將其中的代碼修改成:

static BOOL RCTParseUnused(const char **input)
{
  return RCTReadString(input, "__attribute__((unused))") ||
         RCTReadString(input, "__attribute__((__unused__))") ||
         RCTReadString(input, "__unused");
}

2、Warning: isMounted(...) is deprecated in plain Javascript Classes. Instead, make sure to clean up subscriptions and pending requests in componentWillUnmount to prevent memory leaks.

github上issue : https://github.com/react-navigation/react-navigation/issues/3956,這里的issue說的很明白了,這個問題不是組件本身的問題是React Native 的bug.可查看rn的issue里面存在此問題.目前解決辦法就是忽略警告:

import { YellowBox } from 'react-native';

YellowBox.ignoreWarnings(['Warning: isMounted(...) is deprecated', 'Module RCTImageLoader']);

3、Can only update a mounted or mounting component. This usually means you called setState, replaceState, or forceUpdate on an unmounted component. This is a no-op.Please check the code for the xxx component.

這個警告的意思就是我們是在對一個沒有加載的組件上執行了setState({})操作,通常是在剛進頁面還沒有加載完或者離開頁面以后,組件已經被卸載,這樣執行setState時無法找到渲染組件。避免這樣類似的操作,就能解決此類的警告,具體可參考此處解決方案

4、RCTBridge required dispatch_sync to load RCTDevLoadingView. This may lead to deadlocks

參考:解決方案

第一步:修改AppDelegate.m文件

#if RCT_DEV
#import <React/RCTDevLoadingView.h>
#endif
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
  ...
  RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:jsCodeLocation
                                            moduleProvider:nil
                                             launchOptions:launchOptions];
#if RCT_DEV
  [bridge moduleForClass:[RCTDevLoadingView class]];
#endif
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                   moduleName:@"<AddYourAppNameHere>"
                                            initialProperties:nil];
  ...
}

如果修改完不起作用,請重裝APP(* ̄︶ ̄)

5、Xcode10編譯 Build input file cannot be found: '/Users/xxx/.../node_modules/react-native/Libraries/WebSocket/libfishhook.a'

遇到這個問題,可以在圖中位置,找到最后一步,先刪除,然后在重新添加一下就可以了。


示例圖.png

5、 'config.h' file not found Xcode運行,如圖所示

報錯截圖.jpg

解決辦法:

打開命令行,執行如下操作:
1、cd node_modules/react-native/third-party/glog-0.3.4
2、../../scripts/ios-configure-glog.sh
3、Clean項目,重新編譯,解決!

6、RN中發生點擊事件或者頁面顯示的時候,Debug模式下正常,但是在Release模式下,出現卡死或者卡住很長時間的情況。

一般原因:查看是否console.log(),有的話,看是否在打印類似props,navigate等層級多的內容,這種打印在Release模式下是會卡死的。

7、react-native-render-html iOS端圖片模糊問題。

當iOS端的圖片尺寸較大時候,有時候會導致顯示的圖片模糊。原因應該是圖片加載之前給了一個props.imagesInitialDimensions微小尺寸(默認為100x100)渲染圖像,知道實際尺寸后,圖像會被拉伸。解決方法,對源碼做一下修改。

10c10,11
<             height: props.imagesInitialDimensions.height
---修改為
>             height: props.imagesInitialDimensions.height,
>             indeterminate: (!props.style || !props.style.width || !props.style.height)
80c81,82
<                 height: typeof styleHeight === 'string' && styleHeight.search('%') !== -1 ? styleHeight : parseInt(styleHeight, 10)
---修改為
>                 height: typeof styleHeight === 'string' && styleHeight.search('%') !== -1 ? styleHeight : parseInt(styleHeight, 10),
>                 indeterminate: false
92c94
<                 this.setState({ width: optimalWidth, height: optimalHeight, error: false });
---修改為
>                 this.setState({ width: optimalWidth, height: optimalHeight, indeterminate: false, error: false });
117a120,125
---新增
>     get placeholderImage () {
>         return (
>             <View style={{width: this.props.imagesInitialDimensions.width, height: this.props.imagesInitialDimensions.height}} />
>         );
>     }
> 
120,121c128,134
< 
<         return !this.state.error ? this.validImage(source, style, passProps) : this.errorImage;
------修改為
>         if (this.state.error) {
>             return this.errorImage;
>         } 
>         if (this.state.indeterminate) {
>             return this.placeholderImage;
>         }
>         return this.validImage(source, style, passProps);

8、報錯:Error: Cannot find module 'asap/raw'。

新建項目,可能出現這種錯誤,只需要重新執行一下:npm install 即可!


錯誤截圖.jpg

9、android打包apk錯誤:Could not list contents of '/Users/xxxx/Downloads/xxxx/node_modules/node-pre-gyp/node_modules/.bin/needle'. Couldn't follow symbolic link.

解決辦法:

打開命令行,執行如下操作:
1、defaults write com.apple.finder AppleShowAllFiles -bool true  命令打開Mac的隱藏文件。
2、根據以上的報錯路徑查找node-pre-gyp,把其中的nopt、needle、detct-libc、rc刪掉就行了。
3、defaults write com.apple.finder AppleShowAllFiles -bool false  命令隱藏Mac的隱藏文件。

10、奇葩現象:當我們修改完代碼后,command+R刷新模擬器,頁面是刷新了,可以新改的代碼并沒有同步,于是進行了清緩存、重啟電腦等等一系列操作,還是無效!

解決辦法:

打開命令行,執行如下操作:
1、defaults write com.apple.finder AppleShowAllFiles -bool true  命令打開Mac的隱藏文件。
2、在項目目錄下找到.git/index.lock文件,刪掉,OK,大功告成!
3、defaults write com.apple.finder AppleShowAllFiles -bool false  命令隱藏Mac的隱藏文件。

歡迎大家加入RN討論群,一起填坑:670055368

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。