从标题反应本地呼叫屏幕功能

时间:2019-01-06 04:52:54

标签: javascript react-native react-navigation

我在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);

ScreenPassword

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>
        )
    }
}

2 个答案:

答案 0 :(得分:2)

您可以使用参数和静态方法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 = () => {
    ...
  }

}

答案 1 :(得分:0)

请告诉我如何在 React Native v0.6 或更高版本中实现这项工作?

function Router() {
      return (
        <Stack.Navigator initialRouteName="Home" headerMode="float">
          <Stack.Screen
            name="Home"
            component={HomeTabs}
            options={{
              headerTitle: 'Go to year',
              headerTitleContainerStyle: {
                left: 0,
                right: 0,
              },
              headerRight: () => (
                <View>
                  <TextInput
                    style={styles['search-input']}
                    keyboardType="numeric"
                    maxLength={4}
                    onChangeText={(year) => methodToCall(year)}
                  />
                  <IconSearch style={styles['header-icon-search']} />
                </View>
              ),
            }}
          />
          <Stack.Screen name="Month" component={MonthScreen} />
        </Stack.Navigator>
      );
    }