我有一个项目的反应-本机(0.23)与Meteor 1.3作为后端,并希望显示一个联系项目列表。当用户单击联系人项时,我希望在项目前面显示一个复选标记。
对于与Meteor的连接,我使用可怕的库进展中-团队/反应-原生流星。
import Meteor, { connectMeteor, MeteorListView, MeteorComplexListView } from 'react-native-meteor';
class ContactsPicker extends React.Component {
constructor(props) {
super(props);
this.state = {
subscriptionIsReady: false
};
}
componentWillMount() {
const handle = db.subscribe("contacts");
this.setState({
subscriptionIsReady: handle.ready()
});
}
render() {
const {subscriptionIsReady} = this.state;
return (
<View style={gs.standardView}>
{!subscriptionIsReady && <Text>Not ready</Text>}
<MeteorComplexListView
elements={()=>{return Meteor.collection('contacts').find()}}
renderRow={this.renderItem.bind(this)}
/>
</View>
);
}第一个问题是,一旦subscriptionIsReady返回true,它就不会触发重呈现。如何等待订阅做好准备,然后更新模板?
我的第二个问题是,单击列表项会更新状态,并且应该显示一个复选标记,但是如果MeteorListView已经更改,那么dataSource只能重新呈现。是否有任何方法强制重新呈现而不更改/更新dataSource?
编辑1(解决方案1):
谢谢你@user1078150提供了一个有效的解决方案。在这里,完整的解决方案是:
'use strict';
import Meteor, { connectMeteor, MeteorListView, MeteorComplexListView } from 'react-native-meteor';
class ContactsPicker extends React.Component {
getMeteorData() {
const handle = Meteor.subscribe("contacts");
return {
subscriptionIsReady: handle.ready()
};
}
constructor(props) {
super(props);
this.state = {
subscriptionIsReady: false
};
}
componentWillMount() {
// NO SUBSCRIPTION HERE
}
renderItem(contact) {
return (
<View key={contact._id}>
<TouchableHighlight onPress={() => this.toggleSelection(contact._id)}>
<View>
{this.state.selectedContacts.indexOf(contact._id) > -1 && <Icon />}
<Text>{contact.displayName}</Text>
</View>
</TouchableHighlight>
</View>
)
}
render() {
const {subscriptionIsReady} = this.data;
return (
<View>
{!subscriptionIsReady && <Text>Not ready</Text>}
<MeteorComplexListView
elements={()=>{return Meteor.collection('contacts').find()}}
renderRow={this.renderItem.bind(this)}
/>
</View>
);
}
}
connectMeteor(ContactsPicker);
export default ContactsPicker;发布于 2016-04-20 09:53:36
你必须这样做:
getMeteorData() {
const handle = Meteor.subscribe("contacts");
return {
ready: handle.ready()
};
}
render() {
const { ready } = this.data;
}https://stackoverflow.com/questions/36739862
复制相似问题