【发布时间】:2015-11-12 00:27:19
【问题描述】:
您可以在下面找到相关的代码和屏幕截图。还有一个working demo at rnplay.org
简而言之,这就是问题所在。我有一个列表视图。每行的日期都是动态的,基于对存储在 state 中的对象的映射。当我单击 Cycle Dates 按钮时,我替换了应该更新每一行日期的对象 this.state.cacheTimestamps。至少这是我期望发生的。谁能帮我理解为什么实际上没有发生这种情况?
class SampleApp extends React.Component{
constructor(props){
super(props);
this.ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 });
this.state = {
dataSource: this.ds.cloneWithRows(['ABCD','EFGH','IJKL']),
cacheTimestamps: {},
cacheLotery: [
{'ABCD': 'Jan 1, 1970'},
{'ABCD': 'Jan 2, 1970', 'EFGH': 'Jan 2, 1971'},
{'ABCD': 'Jan 3, 1970', 'EFGH': 'Jan 3, 1971', 'IJKL': 'Jan 3, 1972'},
],
}
// this.randomIntBetween.bind(this);
}
componentWillMount(){
this.setState({
cacheTimestamps: this.state.cacheLotery[this.randomIntBetween(0,2)]
})
}
row( id ){
let lastCached = (this.state.cacheTimestamps[id]) ? this.state.cacheTimestamps[id] : 'never';
return(
<View>
<Text style={styles.label}>
{id}
</Text>
<Text style={styles.cache}>
{lastCached}
</Text>
</View>
)
}
cycleDates(){
const min = 0;
const max = 2;
const idx = this.randomIntBetween(min,max);
this.setState({
cacheTimestamps: this.state.cacheLotery[ idx ]
}, ()=> console.log('dbug cycleDates, new index [%i] and cacheTimestamps Object is %O', idx, this.state.cacheTimestamps));
}
randomIntBetween(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
render() {
return (
<View style={styles.container}>
<ListView
dataSource={this.state.dataSource}
renderRow={this.row.bind(this)}
/>
<TouchableHighlight
onPress={ ()=>this.cycleDates() } >
<Text style={styles.buttonTxt}>
Cycle Dates
</Text>
</TouchableHighlight>
</View>
);
}
};
顺便说一句,考虑到它可能没有将新对象视为不同的对象,我也尝试了Object.assign 以确保它是一个全新的对象..
cycleDates(){
const min = 0;
const max = 2;
const idx = this.randomIntBetween(min,max);
this.setState({
cacheTimestamps: Object.assign({}, this.state.cacheLotery[ idx ])
}, ()=> console.log('dbug cycleDates, new index [%i] and cacheTimestamps Object is %O', idx, this.state.cacheTimestamps));
}
【问题讨论】:
标签: listview reactjs react-native