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

[Question] How to update MKButton label based on state change? #396

Open
rat-moonshine opened this issue Jun 29, 2018 · 1 comment
Open

Comments

@rat-moonshine
Copy link

Hi,

I'm new to React Native, and working on an experimental app where I'm using MKButton. I have asked the same question at StackOverflow but didn't had any proper solution, so I thought to ask you here.

Based on my limited knowledge, I've constructed the MKButton in following way:

import React, { Component } from 'react';
import {
  StyleSheet,
  Text,
  View,
  ScrollView
} from 'react-native';
import Icon from 'react-native-vector-icons/EvilIcons';
import {MKTextField, MKColor, MKButton} from 'react-native-material-kit';
import {connect} from 'react-redux';
import * as actions from '../actions';

const getButtonLabel = () =>
{
   if (!this.props.toUpdate)
   {
       return 'ADD';
   }
   else
   {
       return 'UPDATE';
   }
}
    
const AddButton = MKButton.coloredButton()
    .withText({getButtonLabel()}).build();

type Props = {};
class AddPerson extends Component<Props> 
{
    render() {
        return (
            return (
            <View style={styles.addButton}>
                <AddButton onPress={this.onButtonPress.bind(this)}/>
            </View>
        );
    }
}

const mapStateToProps = state => {
    return {
        toUpdate: state.toUpdate,
    };
};

export default connect(mapStateToProps, actions)(AddPerson);

But I couldn't able to get it work due to different undefined reference errors at different times, or just no rendering of the button at all.

I see there's a way documented at here, but this also so much of writing and not renders same as button created with MKButton.coloredButton().build(), unfortunately:

<MKButton
  backgroundColor={MKColor.Teal}
  shadowRadius={2}
  shadowOffset={{width:0, height:2}}
  shadowOpacity={.7}
  shadowColor="black"
  onPress={() => {
    console.log('hi, raised button!');
  }}
  >
  <Text pointerEvents="none"
        style={{color: 'white', fontWeight: 'bold',}}>
    RAISED BUTTON
  </Text>
</MKButton>

Can we do this in a .build() way and also change the label based on state change?

Thanks!

@kientux
Copy link

kientux commented Sep 10, 2018

I don't think there is a way, because the builder version creates a Text internally and doesn't expose any reference outside. If you want it to be the same as builder version, try reading the source code to see what it is styled inside:

function coloredRaisedButton() {
  return new ButtonBuilder()
    .withStyle({
      borderRadius: 2,
      shadowRadius: 1,
      shadowOffset: { width: 0, height: 0.5 },
      shadowOpacity: 0.7,
      shadowColor: 'black',
      elevation: 4,
    })
    .withTextStyle({
      color: 'white',
      fontWeight: 'bold',
    });
}

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