question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

setting selectedOption for options as an object

See original GitHub issue

Using segmented controls for options as value pairs i cant get the selected option to highlight the selected field.

My on selection if firing and it is returning the value in the text below but the selected field remains unstyled any advice?

I tried setting the selected field to both the value and the label and indeed the object itself.

Also i have bound all the functions in the contructor to “this”

 setSelectedOption(option){
    console.log(option.label)
   this.setState({selectedOption: option})
     
    }

    
  renderOption(option, selected, onSelect, index){
    const style = selected ? { fontWeight: 'bold'} : {};
 
    return (
      <TouchableWithoutFeedback onPress={onSelect} key={index}>
        <Text style={style}>{option}</Text>
      </TouchableWithoutFeedback>
    );
  }

  renderContainer(optionNodes){
    return <View>{optionNodes}</View>;
  }  




 const options = [
  {
    label: 'Walking',
    value: 'walking'
  },
  {
    label: 'Driving',
    value: 'driving'
  },
]



     return (
 <SegmentedControls
          options={ options }
          onSelection={ (option) => this.setSelectedOption(option) }
          extractText={ (option) => option.label }
          selectedOption={this.state.selectedOption}

        />)


<Text>{this.state.selectedOption.value}</Text>

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
SpaceK33zcommented, Feb 13, 2017

I had the same issue as you. In the source code I found this undocumented prop, testOptionEqual. It seems to check for strict equality by default. This wouldn’t work in my use case, so I changed it to something like this:

<SegmentedControls
  options={[{ label: 'mylabel', value: 'myvalue' }]}
  selectedOption="myvalue"
  testOptionEqual={(selectedValue, option) => selectedValue === option.value}
/>

And now I can just pass values of the options in selectedOption 😃.

0reactions
andreecostacommented, Aug 28, 2017

@hosusan6 @SpaceK33z are you able to change the option after using testOptionEqual={(selectedValue, option) => selectedValue === option.value}?

After using that, the UI won’t update when changing option.

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTMLSelectElement.selectedOptions - Web APIs | MDN
The list of selected options is an HTMLCollection object with one entry per currently selected option. An option is considered selected if ...
Read more >
Angular 4 - using objects for option values in a select list
I'm currently using [ngValue] and it stores objects just fine. The explanation as to why you experienced issues using (change) instead of ...
Read more >
How to Change an HTML Selected Option Using JavaScript
We can change the HTML select element's selected option with JavaScript by setting the value property of the select element object.
Read more >
How to set selected option dynamically in Angular 6 - Medium
Selects in Forms are great when you have multiple options. In Angular as a OPTION value we can use not only string literals,...
Read more >
Option object JavaScript - Dottoro Web Reference
Specifies an item in a selection list. The option element is used to add items to a select element. If the select element...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found