【问题标题】:React Native Call Screen Function from Header从标题中反应本机调用屏幕功能
【发布时间】:2019-01-06 04:52:54
【问题描述】:

我在ScreenPassword 中有更新密码功能,但我想通过点击屏幕标题上的保存按钮来更新密码。

NavSettings.js

const routeConfigs = {
    Password: {
        screen: ScreenPassword,
        navigationOptions: {
            headerTitle: 'Password',
            headerTintColor: '#000',
            headerRight: (
                <View style={styles.headerRight}>
                    <Button
                        style={styles.buttonHeader}
                        color='#000'
                        title="Save"
                        onPress={???????????} />
                </View>
            )
        }
    }
}

export default createStackNavigator(routeConfigs);

屏幕密码

export default class ScreenPassword extends Component {
    updatePassword = () => {

    }
    render() {
        return (
            <ScrollView style={styles.container}>
                <View style={styles.boxForm}>
                    <TextInput
                        style={styles.textInput}
                        placeholder="Old Password"
                        secureTextEntry='true'
                    />
                    <TextInput
                        style={styles.textInput}
                        placeholder="New Password"
                        secureTextEntry='true'
                    />
                    <TextInput
                        style={styles.textInput}
                        placeholder="Confirm Password"
                        secureTextEntry='true'
                    />
                </View>
            </ScrollView>
        )
    }
}

【问题讨论】:

    标签: javascript react-native react-navigation


    【解决方案1】:

    您可以使用 params 和静态方法 navigationOptions:

    class ScreenPassword extends React.Component {
    
      static navigationOptions = ({ navigation }) => {
        return {
          headerTitle: 'Password',
          headerTintColor: '#000',
          headerRight: (
            <View style={styles.headerRight}>
              <Button
                 style={styles.buttonHeader}
                 color='#000'
                 title="Save"
                 onPress={navigation.getParam('updatePassword')}
               />
             </View>
           ),
        };
      };
    
      componentDidMount() {
        this.props.navigation.setParams({ updatePassword: this.updatePassword});
      }
    
      render() {
        ...
      }
    
      updatePassword = () => {
        ...
      }
    
    }
    

    【讨论】:

    • 出现错误 :: TypeError: navigation.getParam is not a function
    猜你喜欢
    • 2022-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多