当前位置: 首页 > 面试题库 >

反应本机性能问题

督烨赫
2023-03-14
问题内容

我正在使用coincap api首先获取大约1500多种加密货币的数据,然后通过网络套接字更新加密货币的更新值。

我正在使用redux在这里管理我的状态

在My内部componentDidMount(),我正在调用 redux动作 fetchCoin来获取硬币的价值

componentDidMount() {
    this.props.fetchCoin()
  }

然后在return我做这样的事情

 <FlatList
           data={this.state.searchCoin ? displaySearchCrypto : this.props.cryptoLoaded}
           renderItem={({ item }) => (
           <CoinCard
              key={item["short"]}
              coinShortName = {item["short"]}
              coinName = {item["long"]}
              coinPrice = {item["price"].toFixed(2)}
              percentChange = {item["perc"].toFixed(2)}
              />

然后我有一个网络套接字,它像这样更新了加密货币的价值

 componentDidUpdate() {
    if (this.state.updateCoinData || this.updateCoinData.length < 1 ) {
      this.updateCoinData = [...this.props.cryptoLoaded];
     this.setState({updateCoinData: true})
    }
      this.socket.on('trades', (tradeMsg) => {
      for (let i=0; i< this.updateCoinData.length; i++) {

        if (this.updateCoinData[i]["short"] == tradeMsg.coin ) {

        //Search for changed Crypto Value
       this.updateCoinData[i]["perc"] = tradeMsg["message"]["msg"]["perc"]
       this.updateCoinData[i]["price"] = tradeMsg['message']['msg']['price']

        //Update the crypto Value state in Redux
        this.props.updateCrypto(this.updateCoinData);

          }
        }
      })
  }

现在,尽管这项工作有效,但问题在于,这使我的应用程序变慢了,因为每当套接字发送新数据时,它都必须渲染每个组件,因此诸如触摸和搜索之类的事件需要大量时间来执行。
[更新] 事实证明,即使我删除套接字连接,我的应用也正在呈现某些内容, 请查看更新2

[问题:] 我应该怎么做才能提高App的性能?(诸如不使用状态或使用DOM来更新我的应用程序之类的东西)。

[更新1:] 我正在使用https://github.com/irohitb/Crypto这两个是所有发生逻辑的js文件
https://github.com/irohitb/Crypto/blob/master/src/container
/cryptoContainer.js

https://github.com/irohitb/Crypto/blob/master/src/components/CoinCard.js
我也从地图转到了Flatlist。

[更新:2] 我发现我的应用程序内部 发生了无休止的渲染 ,这可能使我的线程处于繁忙状态(
我的意思是,这是无休止的并且不必要地传递了props
)。我在单独的上问了同样的问题,但是没有得到正确的答复,并且由于它与性能有关,因此我想在这里悬赏。

[答案更新:] 虽然这里有许多伟大的答案,以防万一有人想了解它是如何工作,你也许可以复制我的仓库,回到 之前
这个承诺。我已将提交链接到解决我的问题的位置(因此您可能需要回去看看我做错了什么)。另外,所有答案都很有用,而且不难理解,因此您一定要仔细阅读。


问题答案:

就像Bhojendra
Rauniyar所说的那样,您应该在CoinCard中使用shouldComponentUpdate。您可能还想更改FlatList,缩小的样本在ScrollView中具有FlatList,这将导致FlatList完全展开,从而立即呈现其所有项目。

class cryptoTicker extends PureComponent {

      componentDidMount() {
        this.socket = openSocket('https://coincap.io');
        this.props.fetchCoin()
        this.props.CurrencyRate()

        this.socket.on('trades', (tradeMsg) => {

            for (let i=0; i< this.updateCoinData.length; i++) {

                if (this.updateCoinData[i]["short"] == tradeMsg.coin ) {

                    //Search for changed Crypto Value
                    this.updateCoinData["short"] = tradeMsg["message"]["msg"]["short"]
                    this.updateCoinData[i]["perc"] = tradeMsg["message"]["msg"]["perc"]
                    this.updateCoinData[i]["price"] = tradeMsg["message"]['msg']['price']

                    //Update the crypto Value state in Redux
                    this.props.updateCrypto(this.updateCoinData);

                }
            }
        })

      }

      componentWillReceiveProps(newProps){
        // Fill with redux data once
        if (this.updateCoinData.length < 1 && newProps.cryptoLoaded) {
            this.updateCoinData = [...newProps.cryptoLoaded];
        }
      }

    render() {

        return (
            <View style={{height: '100%'}}>
                <Header/>
                <FlatList
                    style={{flex:1}}
                    data={this.props.cryptoLoaded}
                    keyExtractor={item => item.short}
                    initialNumToRender={50}
                    windowSize={21}
                    removeClippedSubviews={true}
                    renderItem={({item, index}) => (
                        <CoinCard
                            index={index}
                            {...item}
                        />
                    )}
                />
            </View>
        )
    }
}

class CoinCard extends Component {

    shouldComponentUpdate(nextProps) {
        return this.props.price !== nextProps.price || this.props.perc !== nextProps.perc
    }

    render() {
        console.log("here: " + this.props.index);

        return (
            <View>
                <Text> {this.props.index} = {this.props.long} </Text>
            </View>
        )
    }
}


 类似资料:
  • 问题内容: 我正在React Native中开发一个需要使用Web Map Services的移动应用程序。我尚未找到任何允许同时使用WMS并进行本机响应的库或框架。在React(Web)中,我找到了一个。我的问题是: 您是否知道是否存在允许我使用WMS和React Native的任何库或框架,或者是否有可能在React native中集成React(web)库? 谢谢! 问题答案: 我决定使用的

  • 因此,我得到了一个错误(在底部),很可能是由于我尝试卸载react native pathjs图表引起的。以下问题只存在于android上,而不存在于ios上。 /Users/a.lau/Projects/react-native/First_App/android/app/src/main/java/com/first_app/MainApplication.java: 7:错误:找不到符号导入

  • 我正在尝试为没有经验的霍尼韦尔CT50设备实施React Native Android模块。该模块将监听设备上内置激光扫描仪的扫描。 我已经在React本机站点上完成了正式的演练,我已经成功地设置了一个基本模块,可以在RN组件中接收到一个简单的值。到目前为止,我的代码如下所示: 霍尼韦尔CT50包装: 霍尼韦尔CT50模块: React Native Component(在组件中) 我正在努力理解

  • Raushane@Raushane:~/BigBuilder/Android$./Gradlew assembleRelease 失败:生成失败,出现异常。 > 其中:设置文件'/home/raushang/bigbuilder/Android/Settings.gradle'行:4 有人能建议一下现在该做什么吗。我是新来的本地人。

  • 我们正在构建一个react native 0.61.4应用程序,我们希望使用库react native ssl固定。在ios上运行时,ios证书和获取返回错误“已取消”存在问题。 我们遵循了https://www.npmjs.com/package/react-native-ssl-pinning的步骤。我不确定Usage ios中的第二步:(如果您使用证书钉住,请跳过此步骤)公钥钉住不需要额外的

  • 首先,我是新手 当我构建空的(全新的)项目时,它会给出一个错误 任务:react-nate-gradle-plugin:compileKotlin 'compileJava'任务(当前目标为1.8)和'compileKotlin'任务(当前目标为11)jvm目标兼容性应设置为相同的Java版本。