javascript - 如何从一个对象中访问 this.props?

标签 javascript react-native react-navigation

我正在使用 react-navigation 库的 StackNavigator。我想要一个包含图标和动画文本的标题。但是,部分文本需要根据 this.props 确定,我遇到了一个问题:

static navigationOptions = {
    headerLeft: null,
    headerRight: (
            <View style={{flexDirection: 'row'}}>
                <Animation.Text animation='fadeInRight'
                                delay={2000}
                                duration={1500}
                                style={{
                                    fontFamily: 'Helvetica-Light',
                                    paddingRight: 5,
                                    paddingTop: 10
                                }}>Welcome {JSON.stringify(this.props.navigation.state.params.username)}</Animation.Text>
                <Icon
                    name='ios-contact-outline'
                    type='ionicon'
                    size={30}
                    color={'#77767c'}
                    style={{paddingRight: 5}}
                />
            </View>
    )
}

this.props 在这里未定义,但我不知道如何将对象绑定(bind)到 this 或者这是否可能。我还尝试将调用移出一个函数:

static navigationOptions = {
    headerLeft: null,
    headerRight: (
            <View style={{flexDirection: 'row'}}>
                <Animation.Text animation='fadeInRight'
                                delay={2000}
                                duration={1500}
                                style={{
                                    fontFamily: 'Helvetica-Light',
                                    paddingRight: 5,
                                    paddingTop: 10
                                }}>Welcome {this.getUsername}</Animation.Text>
                <Icon
                    name='ios-contact-outline'
                    type='ionicon'
                    size={30}
                    color={'#77767c'}
                    style={{paddingRight: 5}}
                />
            </View>
    )
}

getUsername() {
    return(JSON.stringify(this.props.navigation.state.params.username))
}

并在我的构造函数中将 getUsername 绑定(bind)到 this,但由于某种原因,该函数从未被调用。

最佳答案

您需要为 navigationOptions 传递一个函数。请查看示例 here , 考虑以下片段

static navigationOptions = props => {
  const { navigation } = props;
  const { state, setParams } = navigation;
  const { params } = state;
  // param has the required value
  return {
    headerLeft: null,
    headerRight: (
        <View style={{flexDirection: 'row'}}>
          ...
        </View>
     )
  }
}

希望这会有所帮助!

关于javascript - 如何从一个对象中访问 this.props?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49756494/

相关文章:

javascript - 多个 facebook 按钮 (fb :like) are not working: unliking themself

javascript - 使用 lodash 从数组中删除两个值(如果重复)

javascript - 在子值更改后重新排序 Polymer dom-repeat

javascript - React Native 固定页脚与 React 导航

react-native - 在 react-native-modal 内导航

通过 ajax 加载时,Javascript 在单独的文件中或与 html 一起运行?

react-native - react native map mapPadding 不工作

javascript - 如何使用 React Native 在循环中生成 <Text/> 组件?

javascript - React Native & Expo,如何控制闪屏?

javascript - 如何区分javascript对象中的url和字符串