【问题标题】:React Component is not re rendering in iOSReact Component 不会在 iOS 中重新渲染
【发布时间】:2021-06-13 12:53:48
【问题描述】:

我有一个自定义标记组件。

export class CustomMarker extends Component {


devicesInRegion() {
    return this.props.devices.filter((device) => device.regionId == this.props.region.regionId)
}

deviceStatus() {
    let devicesInRegion = this.devicesInRegion()
    let criticalCount = 0
    let warningCount = 0
    let goodCount = 0
    let nrCount = 0
    let polling = 0
    
    devicesInRegion.map((device) => {
        if (this.props.critical.includes(device.sensorId)) {
            criticalCount += 1
        } 
        if (this.props.warning.includes(device.sensorId)) {
            warningCount += 1
        } 
        if (this.props.good.includes(device.sensorId)) {
            goodCount += 1
        } 
        if (this.props.NR.includes(device.sensorId)) {
            nrCount += 1
        }
        if (this.props.Polling.includes(device.sensorId)) {
            polling += 1
        }
    })

    this.setState({
        //Gives me Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops. error
    })

    return {
        "critical": criticalCount,
        "warning": warningCount,
        "good": goodCount,
        'NR': nrCount,
        "Polling": polling
    }
}

render() {
    return (

        <View style={{ minWidth: RFPercentage(1.5), backgroundColor: '#fff', alignItems: 'flex-end' }}>
                <Text style={innerStyles.markerText}>{this.deviceStatus().critical}</Text>
                <Text style={innerStyles.markerText}>{this.deviceStatus().warning}</Text>                    
                <Text style={innerStyles.markerText}>{this.deviceStatus().good}</Text>                    
                <Text style={innerStyles.markerText} >{this.deviceStatus().Polling}</Text>
                <Text style={innerStyles.markerText} >{this.deviceStatus().NR}</Text>
        </View>
    )
}
}

我这样传递道具

<CustomMarker
  region={region}
  devices={this.state.userDevices}
  good={this.state.goodList}
  warning={this.state.warningList}
  critical={this.state.criticalList}
  NR={this.state.NRList}
  Polling={this.state.pollingList}
/>

在 Android 中,值会立即更新,但在 iOS 中,值始终显示为零。但是在控制台中正确的值正在打印。任何人都可以建议我,当值更改时我如何重新渲染组件。

更新:

我将自定义标记包装在 react-native-map 中并设置 tracksViewChanges={false} 这使得 CustomMarker 不会在 iOS 中更新。对此有何建议?

【问题讨论】:

  • 它不能只在iOS中工作?
  • 使用 setState() 函数。
  • 嗨乔治,是的,它在 iOS 中没有改变,在 android 中它正在更新,我在 deviceStatus 方法中尝试了 setState,但它给了我 Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops. 错误。我也尝试过功能组件,但没有用。

标签: reactjs react-native render react-native-ios react-component


【解决方案1】:

当你调用 setState 函数改变状态时,React 会重新渲染一个组件(或者 函数组件中 useState 钩子提供的函数)。

因此,子组件仅在父组件的状态已使用这些函数之一更改时更新。

我认为它可以工作。

    export class CustomMarker extends Component {
    constructor(props) {
      super(props);
      this.state = { 
      critical: 0,
      warning: 0,
      good: 0,
      NR: 0,
      Polling: 0
      }

    devicesInRegion() {
       return this.props.devices.filter((device) => device.regionId == this.props.region.regionId)
    }
    
     componentDidMount(){
        let devicesInRegion = this.devicesInRegion()
        let criticalCount = 0
        let warningCount = 0
        let goodCount = 0
        let nrCount = 0
        let polling = 0

        devicesInRegion.map((device) => {
            if (this.props.critical.includes(device.sensorId)) {
                criticalCount += 1
            } 
            if (this.props.warning.includes(device.sensorId)) {
                warningCount += 1
            } 
            if (this.props.good.includes(device.sensorId)) {
                goodCount += 1
            } 
            if (this.props.NR.includes(device.sensorId)) {
                nrCount += 1
            }
            if (this.props.Polling.includes(device.sensorId)) {
                polling += 1
            }
        })

        this.setState({
            critical: criticalCount,
            warning: warningCount,
            good: goodCount,
            NR: nrCount,
            Polling: polling
        });
      } 
                
        render() {
           const {critical,warning,good,NR,Polling} = this.state;
            return (
        
                <View style={{ minWidth: RFPercentage(1.5), backgroundColor: '#fff', alignItems: 'flex-end' }}>
                        <Text style={innerStyles.markerText}>{critical}</Text>
                        <Text style={innerStyles.markerText}>{warning}</Text>                    
                        <Text style={innerStyles.markerText}>{good}</Text>                    
                        <Text style={innerStyles.markerText} >{Polling}</Text>
                        <Text style={innerStyles.markerText} >{NR}</Text>
                </View>
            )
        }
        }

【讨论】:

  • 感谢@George 抽出宝贵时间,但它不起作用。即使我尝试将所有 componentDidMount 代码放在一个函数中并在 setInterval 中循环该函数,它也不会在 UI 中更新。
猜你喜欢
  • 2021-11-24
  • 2017-01-21
  • 2016-05-09
  • 2020-09-27
  • 1970-01-01
  • 2023-03-13
  • 2021-12-17
  • 2020-07-06
  • 1970-01-01
相关资源
最近更新 更多