我认为最简单的就是设置一个状态变量代替你可以玩一个如:
class MyView extends Component {
constructor(props) {
super(props);
this.state = {
activeView: null,
}
this.views = [
(<View style={{ padding: 20 }}><Text>View 1</Text></View>),
(<View style={{ padding: 20 }}><Text>View 2</Text></View>),
(<View style={{ padding: 20 }}><Text>View 3</Text></View>),
(<View style={{ padding: 20 }}><Text>View 4</Text></View>),
(<View style={{ padding: 20 }}><Text>View 5</Text></View>),
];
}
_setActive(index) {
return() => {
this.setState({ activeView: index })
}
}
render() {
return (
<View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
{this.views.map((view, index) => {
let containerStyle = [];
if (index === this.state.activeView) {
containerStyle.push({ borderWidth: 10 })
}
return (
<TouchableOpacity onPress={this._setActive(index)} style={containerStyle}>
{view}
</TouchableOpacity>
);
})}
</View>
);
}
}
您可以使用MyView
,其中作为<MyView />
还对每一个项目requried阵列views
可以有任何所需的风格和配置每个如果处于活动状态只是进入this.state.activeView
。
让我知道如果你有任何问题。