小编sin*_*nan的帖子

ComponentDidUpdate 使用情况和最大更新深度超出

我有一个设置屏幕,我可以从用户那里获取一些信息,例如年龄、体重和性别,在获得这些信息后,我计算用户每天应该喝多少水。

我想自动计算这个金额,而不需要任何计算按钮。

违反不变性:超出最大更新深度。当组件在 componentWillUpdate 或 componentDidUpdate 中重复调用 setState 时,可能会发生这种情况。React 限制嵌套更新的数量以防止无限循环。

我当前计算水量的代码

  //function to calculate how much water to drink
  waterCalculator = () => {
    //let weightPound = this.state.weight * 2.2046;
    let weightValue = this.state.weight;
    let ageValue = this.state.age;
    let waterAmount = null;
    if (ageValue < 30) {
      waterAmount = weightValue * 40;
    } else if (ageValue >= 30 || ageValue <= 55) {
      waterAmount = weightValue * 35;
    } else {
      waterAmount = weightValue * 30;
    }
    this.setState({ sliderValue: waterAmount …
Run Code Online (Sandbox Code Playgroud)

javascript react-native

2
推荐指数
1
解决办法
9558
查看次数

为什么我的 React-Native FlatList 中有相同键的 double ?

我正在尝试获取在以下端点描述的对象:https : //api.cosmicjs.com/v1/67302ce0-7681-11e9-8193-4bd8888ec129/objects? pretty =true&hide_metafields=true

您会注意到有一个带有唯一标识符的 _id 字段。

那么为什么我得到:

“警告:遇到两个具有相同密钥的孩子,:。密钥应该是唯一的,以便组件在更新时保持其身份。非唯一的密钥可能会导致孩子被复制和/或省略——这种行为不受支持,将来可能会改变版本。”

这是我的 FlatList 渲染:

render() {   
    if(this.props.dataToDisplay.objects){
        console.log(typeof(this.props.dataToDisplay.objects))
        console.log(this.props.dataToDisplay.objects)
        this.props.dataToDisplay.objects.forEach((item)=>{
            console.log(item)
        })

        return (
            <View style={[{backgroundColor: this.state.backgroundColor},styles.container]}>
                <FlatList
                    data={this.props.dataToDisplay.objects}
                    keyExtractor={(item, index)=>{item._id.toString()}}
                    renderItem={({item})=>{
                        <Text id={item._id}>{item.title}</Text>
                    }}
                />
            </View>
        );
}
 else {

    return (
        <View style={[{backgroundColor: 
this.state.backgroundColor},styles.container]}>
            <Text>Loading elements.</Text>
        </View>
      );
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

keyExtractor 会不会有问题?我尝试使用 keyExtractor={(item, index)=>{item._id}} 没有结果......

谢谢你的时间。

json react-native react-native-flatlist

2
推荐指数
1
解决办法
2672
查看次数

如何加快 React Native 和 Expo 应用程序的初始启动应用程序

我创建了一个应用程序并添加了启动屏幕。在 Android 模拟器上加载应用程序需要 1 秒。然而,当我在商店中发布该应用程序后,加载需要 4 秒。

对于这样一个简单的应用程序来说,这非常烦人。

我认为这是因为 _loadResourcesAsync 函数加载图片。因此我注释掉了这些行,但没有任何改变。

任何加快我的应用程序启动速度的建议。

在这里你可以找到我的app.js

import React from 'react';
import { Platform, StatusBar, StyleSheet, View } from 'react-native';
import { AppLoading, Asset } from 'expo';
import AppNavigator from './navigation/AppNavigator';

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isLoadingComplete: false,
    };
  }

  render() {
    if (!this.state.isLoadingComplete && !this.props.skipLoadingScreen) {
      return (
        <AppLoading
          startAsync={this._loadResourcesAsync}
          onError={this._handleLoadingError}
          onFinish={this._handleFinishLoading}
        />
      );
    } else {
      return (
        <View style={styles.container}>
          {Platform.OS === 'ios' && …
Run Code Online (Sandbox Code Playgroud)

android splash-screen react-native expo

1
推荐指数
1
解决办法
2123
查看次数