Eya*_*alS 8 realm react-native react-navigation
非常感谢对此的帮助。
我有一个领域 listView,可以导航到详细视图。
详细视图正在从原始列表视图中删除条目并导航回来。我已在领域数据库更改上注册了侦听器以更新列表视图的内容。
这是我正在使用的代码,删除后会出现异常 - 在导航发生后“访问已失效或删除的联系人类型的对象”。
有人知道为什么吗?
另外,似乎更改监听器(updateContactsFromDB)被调用了两次,同时只删除了一个对象 - 想法?
10倍
ContactPage.js:
export default class ContactsPage extends Component {
updateContactsFromDB(){
console.log("ContactsPage:updateContactsFromDB()");
let contacts = Realm.objects('Contact');
this.setState({
dataSource: this.state.dataSource.cloneWithRows(contacts.snapshot()),
});
}
constructor(props) {
console.log("ContactsPage:constructor()");
super(props);
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => (r1 !== r2)});
let contacts = Realm.objects('Contact');
this.state = {
dataSource: ds.cloneWithRows(contacts.snapshot()),
};
this.updateContactsFromDB = this.updateContactsFromDB.bind(this);
}
componentWillMount(props){
console.log("ContactsPage:componentWillMount");
Realm.addListener('change', this.updateContactsFromDB);
}
componentWillUnmount(props){
console.log("ContactsPage:componentWillUnmount");
Realm.removeListener('change', this.updateContactsFromDB);
}
render() {
console.log("ContactsPage:render()");
return (
<ListView
dataSource={this.state.dataSource}
renderRow={(contact) => (
<TouchableOpacity onPress={ () => this.props.navigation.navigate('ContactNotesPage', { contact: contact}) }>
<Text>test Contact</Text>
</TouchableOpacity>
)
}
/>
);
}
}
ContactNotesPage.js:
export default class ContactNotesPage extends Component {
constructor(props) {
console.log("ContactNotesPage:constructor");
super(props);
}
render(){
console.log("ContactNotesPage:render()");
const { params } = this.props.navigation.state;
return (
<TouchableOpacity onPress={ () => {
console.log("ContactNotesPage:delete");
Realm.write(() => { Realm.delete(params.contact);});
this.props.navigation.navigate('ContactsPage');
}
}>
<Text>DeleteContact</Text>
</TouchableOpacity>
)
}
};
// main.js
const MainStack = StackNavigator({
ContactsPage: {
screen: ContactsPage,
},
ContactNotesPage:{
screen: ContactNotesPage,
},
});
export default MainStack;
Run Code Online (Sandbox Code Playgroud)
看起来这是领域/反应导航中的一个错误。当传递一个领域对象的参数时 - 如果删除该对象,您的下一次导航将失败。已在https://github.com/realm/realm-js/issues/1031上打开一个错误
| 归档时间: |
|
| 查看次数: |
7811 次 |
| 最近记录: |