concept segmentedcontrolios in category react native

appears as: SegmentedControlIOS
React Native in Action

This is an excerpt from Manning's book React Native in Action.

Figure 10.5 Basic SegmentedControlIOS implementation with two values (one and two)

c10_05.tif

SegmentedControlIOS allows you to access the native iOS UISegmentedControl component. It’s a horizontal tab bar made up of individual buttons, as shown in figure 10.5.

SegmentedControlIOS has the methods and props in table 10.4. At a minimum, it takes an array of values to render the control values, a selectedIndex as the index of the control selected, and an onChange method that will be called when a control is pressed.

Table 10.4 SegmentedControlIOS methods and props
Prop Type Description
enabled Boolean If false, the user can’t interact with the control. Default value is true.
momentary Boolean If true, selecting a segment won’t persist visually.onValueChange will still work as expected.
onChange Function (event) Callback called when the user taps a segment; passes the event as an argument.
onValueChange Function (value) Callback called when the user taps a segment; passes the segment’s value as an argument.
selectedIndex Number Index in props.values of the segment to be (pre)selected.
tintColor String (color) Accent color of the control.
values Array of strings Labels for the control’s segment buttons, in order.

SegmentedControlIOS is a good place to separate and display certain filterable/sortable data in the UI. For example, if an app had information listed and viewable by week, you could use SegmentedControlIOS to separate that data even further by day of the week, with a separate view for each day.

In this example, you’ll render an array of three items as a SegmentedControlIOS. You’ll also show a value in the UI based on which item is selected.

Listing 10.11 SegmentedControlIOS rendering three values
import React, { Component } from 'react'
import { Text, View, SegmentedControlIOS } from 'react-native'    #1  

const values = ['One', 'Two', 'Three']    #2  

class App extends Component {
  
  constructor() {
    super()
    this.state = {
      selectedIndex: 0,    #3  
    }
  }
  
  render() {
    const { selectedIndex } = this.state
    let selectedItem = values[selectedIndex]    #4  
    return (
      <View style={{ marginTop: 40, padding: 20 }}>
        <SegmentedControlIOS    #5  
          values={values}    #5  
          selectedIndex={this.state.selectedIndex}    #5  
          onChange={(event) => {    #5  
            this.setState({selectedIndex:
                event.nativeEvent.selectedSegmentIndex});    #5  
          }}
        />
        <Text>{selectedItem}</Text>    #6  
      </View>)
  }
}

#1   Imports SegmentedControlIOS from React Native
#2   Creates an array of values to use in the SegmentedControlIOS
#3   Creates a state value of selectedIndex set to 0
#4   Creates a variable called selectedItem, set to the value of the selectedIndex of the values array
#5   Renders the SegmentedControlIOS component, passing in the values array as the values prop, this.state.selectedIndex as the selectedIndex, and an onChange method that updates the selectedIndex state value with the index of the pressed item
#6   Renders the value of selectedItem in the UI