【发布时间】:2019-07-25 20:10:18
【问题描述】:
我正在使用 Flatlist 渲染几个盒子。如果“shapes”元素的(属性)“visible”键为 false(定义为 state),则该框将为空。 我不知道这样做是否正确。
现在我希望当我单击框(包裹在 touchableOpacity 中)时,可见值更改为 true,并且框上出现图标。
我想不通这样做。 我不知道我应该在 state 中定义 visible 属性还是稍后动态添加它。
这是我的代码
const { width, height } = Dimensions.get('window')
class App extends Component {
state = {
shapes: [
{ id: 1, icon: 'home', color: 'green', visible: false },
{ id: 2, icon: 'creditcard', color: 'red', visible: false },
{ id: 3, icon: 'star', color: 'purple', visible: true },
{ id: 3, icon: 'notification', color: 'blue', visible: false },
]
}
showShapes = () => {
for (let e of this.state.shapes) {
e.visible = true
console.log(e.visible)
}
this.setState({
// what to write here
})
}
renderItems = ({ item }) => {
return (
<TouchableOpacity
activeOpacity={0.6}
onPress={this.showShapes}
style={{ padding: 10, backgroundColor: '#ccc', margin: 15, width: width / 4 }}
>
{item.visible && <Icon
name={item.icon} color={item.color} size={50}
/> }
</TouchableOpacity>
)
}
render() {
return (
<Fragment>
<FlatList
numColumns='2'
data={this.state.shapes}
renderItem={this.renderItems}
keyExtractor={(item) => item.id.toString()}
/>
</Fragment>
)
}
}
希望我清楚。
【问题讨论】:
标签: javascript reactjs react-native react-native-flatlist