我正在研究<ListView>
組件,我有一個名稱列表,但在更改狀態後,它正在更改值,但它不會重新呈現ListView。ListView在react-native的狀態改變後不會重新渲染?
isSelect:假 - (州)
isSelect:真 - (州)
代碼:
var designerName = [{id: 'Calvin Klein', name: 'Calvin Klein', isSelected: false},
{id: 'Donatella Versace', name: 'Donatella Versace', isSelected: false},
{id: 'Valentino Garavani', name: 'Valentino Garavani', isSelected: false},
{id: 'Giorgio Armani', name: 'Giorgio Armani', isSelected: false}];
export default class filter extends Component {
constructor() {
super();
const listDs = new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 !== r2,
});
this.state = {
listDs:designerName
};
}
componentDidMount(){
this.setState({
designerDataSource:this.state.designerDataSource.cloneWithRows(this.state.listDs),
})
}
render() {
return (
<View>
<ListView
dataSource={this.state.designerDataSource}
renderRow={this.renderRow.bind(this)}
/>
</View>
);
}
renderRow(item){
return (
<TouchableHighlight key={item.id} onPress={this.onItemDesigner.bind(this, item)}>
<View>
<View>
{this.renderName(item)}
</View>
<View>
<Text>{item.name}</Text>
</View>
</View>
</TouchableHighlight>
);
}
renderName(item){
if(item.isSelected) {
return(
<Image
style={{
width: 15,
height:15}}
source={require('../images/black_tick_mark.png')} />
);
}
}
onItemDesigner(item){
var tempDesigner = this.state.listDs.slice();
for(var i=0; i<tempDesigner.length; i++){
if (tempDesigner[i].id == item.id && !tempDesigner[i].isSelected) {
tempDesigner[i].isSelected = true;
}else if (tempDesigner[i].id == item.id && tempDesigner[i].isSelected){
tempDesigner[i].isSelected = false;
}
}
this.setState({
designerDataSource: this.state.designerDataSource.cloneWithRows(tempDesigner),
});
}
}
敬請通過我的上面的代碼,讓我知道,如果你f任何解決方案。
謝謝