reactjs - 从 webview 重定向到 React Native 组件

标签 reactjs react-native

我正在使用 React Native WebView 打开一些复杂的表单步骤。我希望应用程序用户在 WebView 中完成最后一步( http://example.com/completed/ )后重定向 Android 应用程序主页( react native 组件)。

最佳答案

我已经解决了这个问题:

_onLoad(state) {
    //I check the url to see if everything goes right
    if (state.url.indexOf(BASEURL + '/auth/success') != -1) {
      let token = state.url.split("token=")[1];
      token = token.substring(0, token.length - 4);
      //Here I'm caming back
      NavigationsActions.back();
      //In your case you might do something like this:
      NavigationsActions.replaceRoute({
          id: 'your route id'
      });
      SessionActions.setSession(token);
    }
}

我找到了一个很棒的教程,它可以帮助我理解它是如何工作的here .

这是我的整个组件的样子:

import React, { Component } from 'react';
import {
  StyleSheet,
  WebView,
  Text
} from 'react-native';
import NavigationsActions from '../../actions/NavigationsActions';
import NavigationConstants from '../../constants/NavigationConstants';
import RouteConstants from '../../constants/RouteConstants';
import SessionActions from '../../actions/SessionActions';

var BASEURL = 'http://localhost:8080';

class FacebookLogIn extends Component {
  render() {
    return (
      <WebView onNavigationStateChange={this._onLoad} style={styles.container} source={{ uri: BASEURL + '/auth/facebook' }}/>
    );
  }

  _onLoad(state) {
    console.log(state.url);
    if (state.url.indexOf(BASEURL + '/auth/success') != -1) {
      let token = state.url.split("token=")[1];
      token = token.substring(0, token.length - 4);
      NavigationsActions.back();
      SessionActions.setSession(token);
    }
  }
}

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

module.exports = FacebookLogIn;

关于reactjs - 从 webview 重定向到 React Native 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41345083/

相关文章:

css - 我想让它成为一个带有链接标签的点击元素,但我有 css 问题

javascript - 重定向(react router dom)给 Expected 一个赋值或函数调用,而是看到一个表达式 no-unused-expressions

javascript - 在 react 钩子(Hook)上使用 Prop 值(value)不好吗?

javascript - Pan Responder 根本无法在 Android 上运行

reactjs - 在 React 中从父级中的 ajax 调用返回时渲染子组件

javascript - native react "attempt to set value to an immutable object"

react-native - Expo-无法连接到服务器

reactjs - 将 react 应用程序与 react native 应用程序合并

react-native componentWillUnmount 在导航时不起作用

ios - React Native [[DEFAULT]] firebaseapp 未初始化react-native-firebase