首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >状态更改时更新MKButton 'withText‘值

状态更改时更新MKButton 'withText‘值
EN

Stack Overflow用户
提问于 2018-06-28 14:27:48
回答 2查看 142关注 0票数 0

我是React Native的新手,正在开发一个实验性的应用程序,其中我使用React Native Material Kit中的MKButton。

基于我有限的知识,我用以下方式构建了MKButton

代码语言:javascript
复制
const AddButton = MKButton.coloredButton()
    .withText('ADD').build();
class AddUpdateUser extends Component<Props> 
{
    ....
    render() {
        return (
            <View style={styles.addButton}>
                <AddButton onPress={this.onButtonPress.bind(this)}/>
            </View>
        );
}

上面的效果很好。我还想根据状态字段更新该按钮的标签-我尝试了以下方法,但都不起作用:

代码语言:javascript
复制
const AddButton = MKButton.coloredButton()
    .withText(getButtonLabel).build();

const getButtonLabel = () =>
{
    if (!this.props.updateUser)
    {
        return 'ADD';
    }
    else
    {
        return 'UPDATE';
    }
}

谢谢!

EN

Stack Overflow用户

发布于 2018-06-28 18:11:44

代码语言:javascript
复制
const getButtonLabel = () =>
{
    if (!this.props.updateUser)
    {
        return 'ADD';
    }
    else
    {
        return 'UPDATE';
    }
}

const AddButton = MKButton.coloredButton()
    .withText(getButtonLabel()).build();

最佳实践是首先定义getButtonLabel,但我认为它不适合您的原因是getButtonLabel被定义为一个函数,所以您需要在后面加上括号。您可能还需要为JSX编译器使用大括号,但如果不尝试,我就不能100%确定(例如:.withText({getButtonLabel()}).build();)

编辑。现在我们已经看过你的完整文件了,看起来你还没有在类定义中添加方法。因此,让我们将getButtonLabel方法移动到类中封装。这将允许您访问传递给类的props

代码语言:javascript
复制
class AddPerson extends Component {
static navigationOptions = {
    tabBarLabel: 'Add',
    tabBarIcon: ({tintColor}) => (
        <Icon name={'plus'} size={75} style={{color: tintColor}} />
    )
};

shouldComponentUpdate(nextProps, nextState) 
{
    return nextProps.toUpdate != this.props.toUpdate;
}

getButtonLabel = () => {
    if (!this.props.toUpdate)
    {
        return 'ADD';
    }
    else
    {
        return 'UPDATE';
    }
}

onAddButtonPress()
{
    const newOrModifiedPerson = {
        first_name: this.state.first_name,
        last_name: this.state.last_name,
        phone: this.state.phone,
        email: this.state.email,
        company: this.state.company,
        project: this.state.project,
        notes: this.state.notes
    };

    if (!this.props.toUpdate)
    {
        this.props.createNewContact(newOrModifiedPerson);
        this.props.navigation.navigate('PeopleList');
    }
    else
    {
        this.props.updateContact(newOrModifiedPerson);
    }
}

render() {
    return (
        <ScrollView showsVerticalScrollIndicator={false}>
            <View style={styles.container}>
                <Text>{this.props.toUpdate ? 'Update Details' : 'Add New Person'}</Text>
                <MKTextField textInputStyle={styles.fieldStyles}
                    placeholder={'First Name'} value={this.props.first_name}
                    tintColor={MKColor.Teal}
                    onChangeText={first_name => this.setState({first_name})}/>
                <MKTextField textInputStyle={styles.fieldStyles}
                    placeholder={'Last Name'}
                    tintColor={MKColor.Teal}
                    onChangeText={last_name => this.setState({last_name})}/>
                <MKTextField textInputStyle={styles.fieldStyles}
                    placeholder={'Phone Number'}
                    tintColor={MKColor.Teal}
                    onChangeText={phone => this.setState({phone})}/>
                <MKTextField textInputStyle={styles.fieldStyles}
                    placeholder={'Email'}
                    tintColor={MKColor.Teal}
                    onChangeText={email => this.setState({email})}/>
                <MKTextField textInputStyle={styles.fieldStyles}
                    placeholder={'Company'}
                    tintColor={MKColor.Teal}
                    onChangeText={company => this.setState({company})}/>
                <MKTextField textInputStyle={styles.fieldStyles}
                    placeholder={'Project'}
                    tintColor={MKColor.Teal}
                    onChangeText={project => this.setState({project})}/>
                <MKTextField textInputStyle={styles.fieldStyles}
                    placeholder={'Notes'}
                    tintColor={MKColor.Teal}
                    onChangeText={notes => this.setState({notes})}/>
                <View style={styles.addButton}>
                    <AddButton onPress={this.onAddButtonPress.bind(this)}/>
                </View>
            </View>
        </ScrollView>
    );
  }
}

所以你看到这个方法现在是如何在你的类中的了吧?这将使您能够访问this.props,这是您在它之外所不具备的。

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51076037

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档