【发布时间】:2016-08-12 21:35:04
【问题描述】:
我有一个以 Meteor 1.3 作为后端的 react-native (0.23) 项目,并希望显示联系人项目列表。当用户点击一个联系人项目时,我想在项目前面显示一个复选标记。
对于与 Meteor DDP 的连接,我使用了很棒的库 inProgress-team/react-native-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 仅在数据源发生更改时才会重新呈现。有没有办法在不更改/更新数据源的情况下强制重新渲染?
编辑 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;
【问题讨论】:
标签: javascript meteor react-native ecmascript-6 ddp