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.

ListItem Switch Component doesn't stay switched on

See original GitHub issue

From the Snack that was referenced in #256, I’m seeing the switch reverting to off each time it is switched on.

Adding switchButton to my code and testing in the iOS simulator reproduced the same issue.

Steps to reproduce:

  1. Open https://snack.expo.io/SJPQNScUZ. Turn switch in row 2 on.
  2. Switch should stay on
  3. Switch turns off

Using RNE version 0.18.4

<List>
    <FlatList style={{width: viewportWidth}}
        data={this.state.contacts}
        ListHeaderComponent={this.renderContactListHeader}
        renderItem = {({item}) => renderListItem(item)}
        keyExtractor={item => item.id}
    />
</List>
renderListItem = (item) => {
    const li = <ListItem
        hideChevron
        switchButton
        title={`${item.firstName} ${item.lastName}`}
        subtitle={item.emails[0].email}
        onPress={() => handleContactPress(item)}
    />;
    return li;
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6

github_iconTop GitHub Comments

6reactions
xavier-villelegiercommented, Nov 27, 2017

As it is specified in the React Native Switch documentation, the Switch component is a controlled component. This means that you have to handle by yourself the state of the component to reflect user actions, by using the onValueChange callback and updating an internal state. You’ll then use this state as the value props of the Switch component 🙌 Thanks for the Snack @jnpdx !

0reactions
evanmcdcommented, Mar 31, 2018

Sorry for the delay - I’m just getting back to this functionality in my app now. Appreciate the info @jnpdx. The example you provided works for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native elements list item switch not working
To fix this, you need to hold the switched value for each ListItem somewhere; probably the most straightforward would be in what you...
Read more >
Switch - React Native
This is a controlled component that requires an onValueChange callback that updates the value prop in order for the component to reflect user ......
Read more >
Create interactive components with variants - Figma Help Center
Create variant interactions · Select the starting variant for the interaction from within the component set. · Navigate to the Prototype tab of...
Read more >
React key attribute: best practices for performant lists
Looking into how React "key" attribute works, how to use it correctly, how to improve performance of lists with it, and why array...
Read more >
8 common React error messages and how to address them
This is caused by a state update that is dependent on an async request. The async request starts somewhere in the lifecycle of...
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