0
この例では、TouchableOpacity
ボタンのセットを使用してListView
を作成しています。そのうちの1つだけが青色で一度に強調表示される必要があります。背景色が再レンダリングされない
式は、背景色を決定します
backgroundColor:rowData.key==this.state.selected?'blue':'transparent'
問題がrender()
メソッドが呼び出されると、状態が更新されているにもかかわらず、backgroundColor
が変化しないということです。
私はforceUpdate()
を試したこともあります。この場合、2回レンダリングされますが、色は更新されません。
Renderメソッド:
render() {
console.log("RE-RENDER",this.state.selected);//GETS CALLED, STATE UPDATED
return (
<ListView
dataSource={this.state.dataSource}
keyboardShouldPersistTaps={true}
renderRow={(rowData) =>
rowData.visible &&
<TouchableOpacity
style={{backgroundColor:rowData.key==this.state.selected?'blue':'transparent'}}
onPress={(evt) => {
this.setState({
selected:rowData.key,
}, function() {
console.log("NEW",this.state.selected);
this.forceUpdate();//DOESN'T HELP
});
}}
>
<Text>{rowData.value}</Text>
</TouchableOpacity>
}
automaticallyAdjustContentInsets={false}
/>
);
};
コンストラクタ:
constructor(props) {
super(props);
var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
dataSource: ds.cloneWithRows([
{
key: 'first',
value: 'BMW',
visible: true,
},{
key: 'second',
value: 'Mercedes',
visible: true,
},
]),
};
}