简体   繁体   English

从浏览器表单提交按钮重定向到应用程序本机响应

[英]Redirect from browser form submit button to app react native

I have a TypeForm integrated with my app and this type form opens in a WebView. 我有一个与我的应用程序集成的TypeForm,并且此类型表单在WebView中打开。 The process goes like when I Sign up, a type form opens in a WebView. 该过程类似于当我注册时,在WebView中打开一个类型表单。 On form submit I want it to be redirected to home screen. 在表单提交上,我希望将其重定向到主屏幕。

Here is my code: 这是我的代码:

import React, { Component } from 'react';
import {
  StyleSheet,
  Text,
  View,
  TouchableOpacity,
  ScrollView,
  WebView,
  Linking
} from 'react-native';
import Constants from '../constants';
import NavigationBar from 'react-native-navbar';
import Icon from 'react-native-vector-icons/FontAwesome';
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import * as userActions from '../redux/modules/user';

type Props = {};
class Typeform extends Component<Props> {
  constructor(props){
    super(props);
    this.state={

    }
    // console.log('props ******* next_lottery_time constructor ******** ',props)
  }

  render() {
    const { navigate, goBack } = this.props.navigation;
    const titleConfig = {
      title: 'SURVEY',
      tintColor:Constants.Colors.White
    };
    const uri = 'https://threadest1.typeform.com/to/vjS2Nx';
    return (
      <View style={{flex:1}}>
        <NavigationBar
          title={titleConfig}
          style={{backgroundColor: 'rgb(32,73,157)'}}
          statusBar={{hidden:false,tintColor:'rgb(32,73,157)'}}
          leftButton={<TouchableOpacity style={{marginLeft:Constants.BaseStyle.DEVICE_WIDTH/100*2.5,marginTop:Constants.BaseStyle.DEVICE_HEIGHT/100*.8}} onPress={()=>goBack()}><Icon color={Constants.Colors.White} name='chevron-circle-left' size={30} /></TouchableOpacity>} />
        <WebView
        ref={(ref) => { this.webview = ref; }}
        source={{ uri }}
        onNavigationStateChange={(event) => {
          if (event.url !== uri) {
            this.webview.stopLoading();
            Linking.openURL(event.url);
          }
        }}
      />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    //justifyContent: 'center',
    alignItems: 'center',
  }
});

const mapDispatchToProps = dispatch => ({
  userActions: bindActionCreators(userActions, dispatch)
});

export default connect(null, mapDispatchToProps)(Typeform);

I am using react-navigation for screen navigation purpose. 我正在将react-navigation用于屏幕导航。

This all you can do it in on navigation state change so when you submit from that will return an response and that response you can capture and on that condition you can navigate to another screen. 您可以在导航状态更改中完成所有这些操作,因此当您从该状态提交时,将返回一个响应,并且您可以捕获该响应,并且在这种情况下可以导航到另一个屏幕。

i my scenario i am using for payment with instamojo. 我的情况是我要用instamojo付款。

import React, { Component } from "react";
import { Text, View, StyleSheet, WebView } from "react-native";
import { BASE_URL } from "../../../constants/apiList";
import SecondaryHeader from "../../common/SecondaryHeader";

class Payment extends Component {
  constructor(props) {
    super(props);

    this.state = {
      isLoading: true,
      refreshing: false,
      isPiad: false,
      packages: []
    };
  }
  getParameterByName(name, url) {
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
      results = regex.exec(url);
    if (!results) {
      return null;
    }
    if (!results[2]) {
      return "";
    }
    return decodeURIComponent(results[2].replace(/\+/g, " "));
  }
  onNavigationStateChange = eve => {
    if (eve.loading !== this.state.isLoading) {
      this.setState({
        isLoading: eve.loading
      });
    }
    if (eve.url.indexOf("api/orders/") > -1) {
      this.props.navigation.replace("paymentStatus", {
        url: eve.url,
        id: this.props.navigation.state.params.id
      });
    }
  };
  render() {
    return (
      <View style={styles.container}>
        <SecondaryHeader title="PAYMENT" {...this.props} />
        <WebView
          source={{ uri: this.props.navigation.state.params.url }}
          onNavigationStateChange={this.onNavigationStateChange}
          style={{ height: 200 }}
          startInLoadingState={true}
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1
  }
});

export default Payment;

so after payment instamojo will redirect to payment success of fail page that time i am capturing url and checking if success redirection is there then i am navigating to my payment status screen/ 因此,付款后,instamojo将重定向到失败页面的付款成功,那时我正在捕获url并检查是否存在成功重定向,然后导航至我的付款状态屏幕/

If you are using StackNavigator from react-navigation redirection or switch screens are really easy. 如果您从react-navigation重定向中使用StackNavigator或切换屏幕真的很容易。 you just need to call like the following code after your success response. 成功响应后,您只需要像以下代码一样调用即可。

StackNavigator will look like StackNavigator看起来像

const Stacker = StackNavigator(
        {
            Landing: {
                path: "Landing",
                screen: LandingDrawer
            }
        },
        {
            initialRouteName: this.state.isLoggedIn,
            headerMode: "none",
            navigationOptions: {
                headerVisible: false
            }
        }
    );

Redirection 重定向

const resetAction = NavigationActions.reset({
                        index: 0,
                        actions: [
                            NavigationActions.navigate({
                                routeName: "Landing"
                            })
                        ]
                    });

this.props.navigation.dispatch(resetAction);

this is what I used in my app, hope this will help you 这是我在我的应用程序中使用的,希望对您有帮助

Have you looked at using the React wrapper around Typeform Embed SDK ? 您是否考虑过在Typeform Embed SDK周围使用React包装器

It seems to be the way to go for what you want to achieve. 这似乎是您要实现的目标的方法。

You can listen to onSubmit event that will be triggered when the typeform is submitted and then do the redirection. 您可以侦听提交表单时触发的onSubmit事件,然后执行重定向。

The code would look like this 代码看起来像这样

import React from 'react';
import { ReactTypeformEmbed } from 'react-typeform-embed';

class App extends React.Component {
  render() {
    return <ReactTypeformEmbed url={'https://demo.typeform.com/to/njdbt5'} onSubmit='alert("form submitted");'/>
  }
}

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM