簡體   English   中英

獲得數據和狀態更改后,React-Native不會重新渲染

[英]React-Native don't re-render after getting Data and State changes

我用一個簡單的Api調用構建了一個用於顯示圖像和注釋的應用程序。 好久好。 在我的一個組件上,我給了Image Url並對其進行了渲染,並且一切正常,但是在獲得注釋和setState之后,該組件不會重新渲染並顯示注釋。 誰能看看我說錯了嗎

非常感謝

import React, { Component } from 'react';
import { ScrollView, View, Text, StyleSheet, Alert } from 'react-native';
import lighthouseStyles from './lighthouse.styles';
import PhotoView from 'react-native-photo-view';
import { FooterTab } from 'native-base';
import Icon from 'react-native-vector-icons/Entypo';
import UserLifeMemoriesManager from '../Manager/UserLifeMemoriesManager';

export default class SingleView extends Component {
  constructor(props) {
    super(props);
    let url = this.props.navigation.getParam('ImageUrl', 'ups not work ');
    let id = this.props.navigation.getParam('id', 0);
    this.state = {
      Imageurl: url,
      arrayComments: '',
      id: id
    };
    UserLifeMemoriesManager.getMemoryDetails(this.state.id).then(Details => {
      this.setstate = {
        arrayComments: Details,
        commentsArray: Details.response.data.memory.comments
      };
    });
  }

  deleteMemory() {
    Alert.alert(
      'löschen',
      'Erinnerung wirklich löschen?',
      [
        {
          text: 'Cancel',
          onPress: () => console.log('Cancel Pressed'),
          style: 'cancel'
        },
        {
          text: 'OK',
          onPress: () => {
            UserLifeMemoriesManager.deleteMemory(this.state.id).then(
              response => {
                this.props.navigation.goBack();
                console.log(response);
              }
            );
          }
        }
      ],
      { cancelable: false }
    );
  }

  WholeComments() {
    if (this.state.commentsArray) {
      return this.state.commentsArray.map(function(news, i) {
        return (
          <View key={i}>
            <Text>{news.created}</Text>
            <View>
              <Text>{news.content}</Text>
            </View>
          </View>
        );
      });
    }
  }

  render() {
    return (
      <ScrollView sytle={styles.container}>
        <PhotoView
          source={{ uri: this.state.Imageurl }}
          minimumZoomScale={1}
          maximumZoomScale={3}
          androidScaleType="fitCenter"
          onLoad={() => console.log('Image loaded!')}
          style={{ width: 420, height: 550 }}
        />

        {this.WholeComments()}

        <View style={{ height: 42 }}>
          <FooterTab style={{ backgroundColor: '#dadcce' }}>
            <Icon.Button
              backgroundColor="#dadcce"
              color="#000000"
              name="bucket"
              size={30}
              marginLeft={12}
              padding={6}
              onPress={() => {
                this.deleteMemory();
              }}
            />
          </FooterTab>
        </View>
      </ScrollView>
    );
  }
}

SingleView.navigationOptions = ({ navigation }) => ({
  title: 'SingleView'
});

const styles = StyleSheet.create(lighthouseStyles, {
  container: {
    justifyContent: 'center',
    alignItems: 'center'
  }
});

您有錯別字錯誤,也是第20行中的函數:

this.setstate = {
   ...
}

它必須是:

this.setState({ 
   ... 
})

有關setState函數的更多信息: https : //reactjs.org/docs/react-component.html#setstate

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM