Change Button Color onPress (toggle functionality) React Native

前端 未结 2 1480
执笔经年
执笔经年 2021-02-04 02:45

hope all is well.

I seem to be having difficulty with a basic button functionality. All I need is the state of the class to change and the button style to change every-

2条回答
  •  感动是毒
    2021-02-04 03:03

    I think you should take a step back and do some basic React tutorials before digging too much into React Native - this is a fairly straightforward problem to solve :) Here's a solution for you:

    'use strict';
    
    var React = require('react-native');
    var Dimensions = require('Dimensions');
    var window = Dimensions.get('window');
    
    var {
      AppRegistry,
      StyleSheet,
      Text,
      View,
      NavigatorIOS,
      Image,
      TouchableHighlight,
      TextInput,
    } = React;
    
    class ToggleButton extends React.Component {
    
      render() {
        return (
          
              
                
                  {this.props.label}
                
              
          
        );
      }
    }
    
    
    class LS1 extends React.Component{
    
      constructor(props){
        super(props);
        this.state = {
          paleo: false,
          vegan: false,
          vegetarian: false,
        }
      }
    
      updateChoice(type) {
        let newState = {...this.state};
        newState[type] = !newState[type];
        this.setState(newState);
      }
    
      SkipLogin() {
        var num = window.height/8.335;
        console.log(num);
      }
    
      render() {
        return (
          
            
              Help us get to know your dietary lifestyle.
    
              
                
                   { this.updateChoice('vegan') }} selected={this.state.vegan} />
                   { this.updateChoice('paleo') }} selected={this.state.paleo} />
                   { this.updateChoice('vegetarian') }} selected={this.state.vegetarian} />
                
              
    
              
                
                  skip this step
                
              
    
            
          
        );
      }
    };
    
    var styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: 'transparent'
      },
      bgImage: {
        flex: 1,
        width: window.width,
        resizeMode: 'cover',
      },
      icontext: {
        color: '#5d5d5d',
        fontWeight: '400',
        fontSize: 20,
        backgroundColor: 'transparent',
        paddingLeft: 10,
        alignItems: 'center',
        marginTop: window.height/2.2,
        textAlign: 'center',
        margin: 10,
      },
      bubblechoice_click: {
        height: window.height/8.335,
        borderRadius: (window.height/8.3350)/2,
        marginRight: 2,
        width: window.height/8.335,
      },
      bubblechoice: {
        height: window.height/8.335,
        borderRadius: (window.height/8.3350)/2,
        marginRight: 2,
        width: window.height/8.335,
      },
      row: {
        flex: 1,
        alignItems: 'center',
        flexDirection: 'row',
        justifyContent: 'center',
        marginTop: -30,
      },
      choicetext: {
        alignItems: 'center',
        alignSelf: 'center',
        color: 'white',
        marginTop: 35,
        fontWeight: '600',
        marginLeft: -18,
        fontSize: 14,
        flex: 1,
        textAlign: 'center'
      },
      overlay: {
        backgroundColor:'rgba(80,94,104,0.7)',
        height: 100,
        width: 100,
        alignItems:'center'
      },
    
    });
    
    module.exports = LS1;
    
    AppRegistry.registerComponent('main', () => LS1);
    

    You can try it out by downloading Exponent to your phone from http://exponentjs.com/ (app store or beta, whichever you prefer) then loading up exp://exp.host/@brentvatne/button-color-exp

提交回复
热议问题