【问题标题】:React-Native don't re-render after getting Data and State changesReact-Native 在获取数据和状态更改后不会重新渲染
【发布时间】:2018-10-18 11:27:45
【问题描述】:

我使用简单的 Api 调用构建了一个应用程序,用于显示图像和评论。这么久这么好。在我的一个组件上,我提供了 Image Url 并渲染它们,一切都很好,但是在我得到 cmets 和 setState 之后,组件不会重新渲染并显示 cmets。 任何人都可以看看并说我做错了什么

非常感谢

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'
  }
});

【问题讨论】:

    标签: javascript android api react-native mobile


    【解决方案1】:

    你有一个拼写错误,也是第 20 行中的一个函数:

    this.setstate = {
       ...
    }
    

    它必须是:

    this.setState({ 
       ... 
    })
    

    更多关于 setState 函数的信息:https://reactjs.org/docs/react-component.html#setstate

    【讨论】:

    • danke es funktioniert man ist manchmal einfach absolut blind das ist fast etwas peinlich :D vielen Dank von Erfurt (und Teilweise Leipzig) nach Leipzig ;)
    • bitteschön von Leipzig :)
    猜你喜欢
    • 2018-12-12
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 2020-03-10
    • 1970-01-01
    • 1970-01-01
    • 2018-11-29
    相关资源
    最近更新 更多