ReactNative开发笔记(持续更新...)

本文详细记录了React Native开发中遇到的各种问题及其解决方案,包括yarn操作问题、View自适应、iOS pod install错误、iOS真机调试问题、滑动View在iOS上的显示问题、react-native-scrollable-tab-view在安卓的闪烁问题、Webview使用问题、RN编译错误、图片加载、热更新等多个方面。每个问题都有清晰的错误描述和具体的解决步骤,是React Native开发者宝贵的参考资料。
摘要由CSDN通过智能技术生成

本文均为RN开发过程中遇到的问题、坑点的分析及解决方案,各问题点之间无关联,希望能帮助读者少走弯路,持续更新中… (2022年11月8日更新)

原文链接:http://www.kovli.com/2018/06/25/rn-anything/

作者:Kovli

- yarn 操作时遇到Integrity check failed for ...(computed integrity doesn't match our records, got "sha512-... sha1-..."问题

报错信息

error https://registry.npm.taobao.org/uglify-es/download/uglify-es-3.3.10.tgz: Integrity check failed for "uglify-es" (computed integrity doesn't match our records, got "sha512-r+MU0rfv4L/0eeW3xZrd16t4NZfK8Ld4SWVglYBb7ez5uXFWHuVRs6xCTrf1yirs9a4j4Y27nn7SRfO6v67XsQ== sha1-DBxPBwC+2NvBJM2zBNJZLKID5nc=")
info Visit https://yarnpkg.com/en/docs/cli/remove for documentation about this command.

网上试了各种清缓存方法无效,如果删除lockfile是可以成功,但是会重新生成lockfile,里面的版本号有些会更新,不符合之前锁版本的预期,想要保留locakfile,接下来的才是适合国情的全网最正确解决方案!
解决方案

yarn config set registry https://registry.yarnpkg.com
yarn --update-checksums
yarn config set registry https://registry.npm.taobao.org

- React Native的View自适应内部文本Text宽度

加View的style 增加一个 alignSelf
解决方案

alignSelf: 'flex-start/center/flex-end...',

- iOS pod intall 报错CDN: trunk Repo update failed的解决方案

报错信息

[!] CDN: trunk Repo update failed - 24 error(s):
CDN: trunk URL couldn't be downloaded: https://cdn.jsdelivr.net/cocoa/Specs/3/2/5/FlipperKit/0.128.0/FlipperKit.podspec.json Response: SSL connect error`

解决方案

pod repo remove trunk
pod install

- iOS真机联调红屏报错Error: EISDIR: illegal operation on a directory的解决方案

报错信息

Error: EISDIR: illegal operation on a directory, read
    at Object.readSync (fs.js:568:3)
    at tryReadSync (fs.js:353:20)
    at Object.readFileSync (fs.js:390:19)
  • 临时解决方案:摇一摇iPhone,弹出debug菜单,倒数第二项点进去,分别输入PC端ip地址,8081,index,然后再reload就可以了
  • 问题根源在debug模式没有找到index bundle文件,检查iOS原生部分jsCodeLocation的获取是否有问题,例如下面的获取方式里index是否写成了index.ios的老版本写法
        jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];

- RN的层叠关系中,一个滑动View的zIndex设置为2,当滑动到其父View的兄弟View时,iOS的子View会被盖住,安卓正常的解决方案

在叠层关系中,子View的zIndex不能高于父View的zIndex,故而导致子View了zIndex设置无效;
解决方法:

提升该View的父View的zIndex 为2,再提升子View的zIndex才可生效;

PS:要zIndex生效,还要设置子view的position的位置。

- react-native-scrollable-tab-view的goToPage方法在安卓端出现先返回第一页再跳转到指定页的闪烁问题的解决方案

解决方案
1、<ScrollAbleView增加 prerenderingSiblingsNumber = {Infinity}
Infinity可修改为所需的数字
prerenderingSiblingsNumber (Integer) - pre-render nearby # sibling, Infinity === render all the siblings, default to 0 === render current page.

2、跳转增加setTimeOut,否则就停在第一页不跳了

        setTimeout(() => {
            this.goToPage(1);
        }, 0);

- react-native-webview 使用injectedJavaScript方法注入网页js代码时的坑点:

1、获取dom元素当document.getElementById('loginId').value = "test-mobile"失效时采用这种方式获取:

const input = document.querySelector('#loginId');
Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value')
            .set.call(input, storage["loginId"] || "");

2、安卓有效,iOS的injectedJavaScript不生效的解决方案:
增加onMessage

injectedJavaScript={jsCode}
onMessage={(event) => {}}

3、安卓有效,iOS的document.getElementByIdObject.getOwnPropertyDescriptor均无效的解决方案:

Fixed using a timer.

const jsCode = `
    setTimeout(() => {
        document.getElementById("name").value = "${name}"; 
        document.f1.submit(); 
    }, 100);
`;

解决方案参考地址:https://github.com/react-native-webview/react-native-webview/issues/341

完整案例:

        let jsCode = `
        // document.getElementById('loginId').value = "test-mobile";
        // document.getElementById('password').value = "test123";
        var storage=window.localStorage;
        if(storage){
          setTimeout(() => {
           const input = document.querySelector('#loginId');
           const pwd = document.querySelector('#password');
           Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value')
            .set.call(input, storage["loginId"] || "");
           if (storage["password"]) {
             Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value')
              .set.call(pwd, window.atob(storage["password"]) || "");
           }
           input.dispatchEvent(new Event('change', { bubbles: true }));
           pwd.dispatchEvent(new Event('change', { bubbles: true }));
           document.querySelector('#loginBtn').onclick = function(){
            if (document.getElementById('loginId').value && document.getElementById('password').value) {
                  //写入loginId字段
                  storage["loginId"]=document.getElementById('loginId').value;
                  //写入password字段
                  storage.password=window.btoa(document.getElementById('password').value);
             }
           };
          }, 100);
        }
         `;
        let isSomePage = false
        return (
            <WebView
                ref={webview => {
                    this.webview = webview;
                }}
                style={
  {
                    backgroundColor: UIConstant.Common_Color_Background,
                }}
                source={
  {uri: this.props.url}}
                injectedJavaScript={jsCode}
                onMessage={(event) => {}}
                // thirdPartyCookiesEnabled={true}
                // domStorageEnabled={true}
                // allowFileAccess={true}
                // cacheEnabled={true}
                // sharedCookiesEnabled={true}
                // sharedCookiesEnabled={true}
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值