Android React Native中的数据更改后组件未更新

Android React Native中的数据更改后组件未更新,android,reactjs,react-native,Android,Reactjs,React Native,让我先说一句,我是新来的反应者,而且是本地人,所以我可能会犯一些明显的错误 我使用React原生模块()访问手机的联系人列表,然后尝试在列表视图中显示这些联系人。但是,在emulator(Genymotion)中运行我的应用程序后,它不会根据我对contacts数组所做的数据更改来更新Listview 这是我的index.android.js: import React, {AppRegistry, Component, StyleSheet, TouchableOpacity, Text,

让我先说一句,我是新来的反应者,而且是本地人,所以我可能会犯一些明显的错误

我使用React原生模块()访问手机的联系人列表,然后尝试在列表视图中显示这些联系人。但是,在emulator(Genymotion)中运行我的应用程序后,它不会根据我对contacts数组所做的数据更改来更新Listview

这是我的index.android.js:

import React, {AppRegistry, Component, StyleSheet, TouchableOpacity, Text, 

View, ListView} from 'react-native';

var l_mContacts = require('react-native-contacts');

var l_cAllContacts = [ ];

var newPerson = {
  familyName: "Nietzsche",
  givenName: "Friedrich",
  emailAddresses: [{
    label: "work",
    email: "mrniet@example.com",
    number: "123456789"
  }]
}; //sample person

  l_mContacts.addContact(newPerson, (err, contacts) => { 
  if(err && err.type === 'permissionDenied'){
    alert("error");
  } else {
  }

  }) 



  l_mContacts.getAll((err, contacts) => {
    if(err && err.type === 'permissionDenied'){
      alert("error");
    } else {
      l_cAllContacts = contacts; //data changes here
      alert(contacts); //I can successfully see the sample contact I added here
    }
  });

class ReactNativeTest extends Component {
  constructor(props){
    super(props)
    var ds = new ListView.DataSource({
      rowHasChanged: (r1, r2) => r1 !== r2,
      enableEmptySections: true
    })
    this.state = {
      contactDataSource: ds.cloneWithRows(l_cAllContacts)
    }
  }
  render() {
    return (
      <View>
          <ListView 
          dataSource={this.state.contactDataSource}
          renderRow={(contact) => {return this._renderContactRow(contact)}}
          />
      </View>
    );
  }

  _renderContactRow(contact){
    return (
      <View>
        <Text>{contact ? contact.givenName : "none"}</Text>
      </View>
    )
  }
}

AppRegistry.registerComponent('ReactNativeTest', () => ReactNativeTest);
import React,{AppRegistry,组件,样式表,TouchableOpacity,文本,
视图,ListView}来自“react native”;
var l_mContacts=require('react-native-contacts');
var l_cAllContacts=[];
var newPerson={
家庭名称:“尼采”,
吉文纳姆:“弗里德里希”,
电子邮件地址:[{
标签:“工作”,
电子邮件:“mrniet@example.com",
编号:“123456789”
}]
}; //样本人
l_mContacts.addContact(newPerson,(err,contacts)=>{
如果(err&&err.type==='permissionDenied'){
警报(“错误”);
}否则{
}
}) 
l_mContacts.getAll((错误,联系人)=>{
如果(err&&err.type==='permissionDenied'){
警报(“错误”);
}否则{
l_cAllContacts=contacts;//此处的数据更改
警报(联系人);//我可以成功地看到我在此处添加的示例联系人
}
});
类ReactNativeTest扩展了组件{
建造师(道具){
超级(道具)
var ds=new ListView.DataSource({
行已更改:(r1,r2)=>r1!==r2,
enableEmptySections:true
})
此.state={
contactDataSource:ds.cloneWithRows(l_cAllContacts)
}
}
render(){
返回(
{返回此。\u renderContactRow(联系人)}
/>
);
}
_renderContactRow(联系人){
返回(
{contact?contact.givenName:“无”}
)
}
}
AppRegistry.registerComponent('ReactNativeTest',()=>ReactNativeTest);

知道我做错了什么吗?当我提醒“l_mContacts.getAll”函数中的“contacts”变量时,我看到了预期的结果,但它不会更新组件。非常感谢您的帮助,谢谢。

问题是您在工作中忽略了反应。 让我从程序员的角度快速解释React中的组件是如何工作的:它在开始时渲染一次,如果其状态或道具发生更改,则会导致重新渲染


您要做的是在文件加载时获取要显示的信息。这可能只工作一次,但您不可能获得任何已更改或以某种方式包装在回调中的信息。您可以在构造函数中运行
getAll
方法,并在回调中使用
setState
再次设置contactDataSource状态。

谢谢,我通过您的建议解决了这个问题。我需要使用“componentDidMount”函数。