Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

State not changing switch value #17

Open
bianchi opened this issue Jan 18, 2018 · 1 comment
Open

State not changing switch value #17

bianchi opened this issue Jan 18, 2018 · 1 comment

Comments

@bianchi
Copy link

bianchi commented Jan 18, 2018

I have the following code working fine with the react-native switch. If i change it to use react-native-customisable-switch it stops working. When I press the TouchableOpacity it calls changeSwitchState function but the switch value don't change.

import React, { Component } from 'react';
import { StyleSheet, View, Text, TouchableOpacity, Switch } from 'react-native';
import PropTypes from 'prop-types';

export default class RoomOnOff extends Component {
    static propTypes = {
        name: PropTypes.string.isRequired,
        value: PropTypes.bool.isRequired
    }

    constructor(props){
        super(props);

        this.state = {value: this.props.value};
    }

    changeSwitchState = () =>
    {
        this.setState({value: !this.state.value},() => { console.log('new value = ', this.state.value); });
    }
  
    render() {
        return (
            <TouchableOpacity style={styles.room} onPress={this.changeSwitchState}>
                <Text style={styles.roomText}> {this.props.name} </Text>
                <Switch value={this.state.value} />
            </TouchableOpacity>
        );
    }
}

const styles = StyleSheet.create({
    room: {
        flex: 1,
        flexDirection: 'row',
        justifyContent: 'flex-start',
        alignItems: 'center',
    },
    roomText: {
        flex: 3,
        fontSize: 32,
        fontWeight: 'bold',
    },
});
@arifnyet
Copy link

arifnyet commented Apr 30, 2018

i found a workaround...

componentDidUpdate(prevProps, prevState) {
		// make a reference to switch... in this case this._privacy_setting is the ref
		// compare prev state and current state
		// execute animation method
        if(this._privacy_setting && prevState.privacy_setting != this.state.privacy_setting){
            this._privacy_setting.startGroupAnimations();
        }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants