React-Native中navigator.pop()后如何更新前一个页面

简介: 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010046908/article/details/52453794 1、问题提出React-Native中navigator.pop()后如何更新前一个页面,这是一个最为常见得问题。
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010046908/article/details/52453794

1、问题提出

React-Native中navigator.pop()后如何更新前一个页面,这是一个最为常见得问题。

2、问题的描述

比如说,我们开发应用的时候,上传头像是一个最为常见的功能,我点击选择打开图库的按钮之后,push到图库的页面,我们在上传成功后,需要pop回到当前页面,并把图片路径传到当前页面。

3、React-Native中的解决办法

这个问题对于一个有Android和ios开发经验的程序员首先想到的就是回调函数或者广播机制等。其实在React-Native中同样也可用回调函数来解决这个问题。本来想以android来举例实现,最后还是算了直接上React-Native吧。

  1. 在A页面中实现一个声明一个函数refresView()
  2. 在A页面push参数中增加一个回掉函数callBack(msg)
  3. 在B页面pop时候调用callBack将值传回,更新界面

4.代码的实现

4.A页面的实现

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  Navigator,
  ToastAndroid,
  View
} from 'react-native';
import Button from './Button';
import Test from './test';
var _navigator;
 var d;
class Hello2 extends Component {

  constructor(props){
     super(props);
      d = this;
      this.state = {city:''}
    //  this.refeshView = this.refeshView.bind(this);                      
  }

 configureScene(route){
      return Navigator.SceneConfigs.FadeAndroid;
    }

    refeshView(msg){
    console.log('刷新');
    d.setState({'city':msg});
    console.log('end');
    }

    renderScene(router, navigator){
      console.log(d);
      _navigator = navigator;
      if(router.id === 'main'){
      return <View style={styles.container}>
      <Button  onPress={() => {
               console.log('start');
                _navigator.push({title:'MainPage',id:'page',callBack:(msg)=>{
                 console.log(d);
                 d.refeshView(msg);
                 console.log('end');}});     
          }} text={'打开'} style={styles.instructions} disabled = {false}/>

        <Text style={styles.welcome}>
          {d.state.city}
        </Text>
        <Text style={styles.instructions}>

        </Text>
        <Text style={styles.instructions}>
          Press Cmd+R to reload,{'\n'}
          Cmd+D or shake for dev menu
        </Text>


      </View>
      }else if(router.id === 'page'){

        return (
        <Test navigator={navigator} router={router}/>
      );
      }
    }

  render() {
    return (
 <Navigator
                initialRoute={{ title: 'Main', id:'main'}}
                configureScene={this.configureScene}
                renderScene={this.renderScene} />

    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

AppRegistry.registerComponent('Hello2', () => Hello2);

4.2、B页面的实现

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View
} from 'react-native';
var _navigator;
import Button from './Button';
class Test extends Component {

render() {
    return (
      <View style={{flex:1}}>

        <Button  onPress={() => {
             console.log(this.props);
             this.props.router.callBack('I am a Student');
             this.props.navigator.pop();
          }} text={'返回'} style={styles.instructions} disabled = {false}/>

      </View>
    );

}



}

const styles = StyleSheet.create({
  instructions: {
    textAlign: 'center',
    color: '#126798',
    marginTop: 50,
  }
});

module.exports = Test;

代码非常的简单,谢谢大家学习。

5、效果展示

这里写图片描述

这里写图片描述

这里写图片描述

相关文章
|
存储 前端开发 JavaScript
react怎么实现跨页面传参
react怎么实现跨页面传参
330 2
|
前端开发
react如何跳转页面?
react如何跳转页面?
|
资源调度 JavaScript 前端开发
React-Router 5.0 制作导航栏+页面参数传递
React-Router 5.0 制作导航栏+页面参数传递
103 0
|
10月前
|
前端开发 JavaScript
React项目路由懒加载lazy、Suspense,使第一次打开项目页面变快
本文介绍了在React项目中实现路由懒加载的方法,使用React提供的`lazy`和`Suspense`来优化项目首次加载的速度。通过将路由组件改为懒加载的方式,可以显著减少初始包的大小,从而加快首次加载速度。文章还展示了如何使用`Suspense`组件包裹`Switch`来实现懒加载过程中的fallback效果,并提供了使用前后的加载时间对比,说明了懒加载对性能的提升作用。
622 2
React项目路由懒加载lazy、Suspense,使第一次打开项目页面变快
|
4月前
|
前端开发 JavaScript
除了使用Route组件,React Router还有其他方式处理404错误页面吗
除了使用Route组件,React Router还有其他方式处理404错误页面吗
126 58
|
4月前
|
前端开发
如何在React Router中定义404错误页面组件?
如何在React Router中定义404错误页面组件?
129 57
|
4月前
|
前端开发 UED
在React Router中,如何处理路由的404错误页面?
在React Router中,如何处理路由的404错误页面?
162 57
|
10月前
|
前端开发 JavaScript UED
react或者vue更改用户所属组,将页面所有数据进行替换(解决问题思路)____一个按钮使得页面所有接口重新请求
在React或Vue中,若需在更改用户所属组后更新页面所有数据但不刷新整个页面,可以通过改变路由出口的key值来实现。在用户切换组成功后,更新key值,这会触发React或Vue重新渲染路由出口下的所有组件,从而请求新的数据。这种方法避免了使用`window.location.reload()`导致的页面闪烁,提供了更流畅的用户体验。
112 1
react或者vue更改用户所属组,将页面所有数据进行替换(解决问题思路)____一个按钮使得页面所有接口重新请求
|
10月前
|
前端开发
React页面跳转取消上一个页面的所有请求
React页面跳转时取消上一个页面的所有axios请求,通过axios拦截器设置cancelToken,并在页面跳转时调用cancel函数取消未完成的请求。
197 2
|
10月前
|
前端开发 JavaScript
React配合axios请求拦截校验session,403跳转至登陆页面
React中使用axios进行请求拦截,通过自定义事件监听和响应拦截实现403状态码时的自动登录页面跳转。
335 2