I think the easiest way is to set a state variable so that you can play with it, for example:
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> ); } }
You can use MyView
, where requried as <MyView />
also each element of the views
array can have any desired style and each can be configured if it is active, only by accessing this.state.activeView
.
Let me know if you have any questions.
source share