goBack() 时如何刷新上一个屏幕

How to refresh the previous screen when I goBack()

我是 react-native 的新手。现在我需要在两个屏幕中实现一个功能。当我在 "addActivity" 场景中单击底部时,它将返回到 "addActivity" 的前一个场景 "homeScene" 并显示来自 "addActivity" 场景的数据。

我尝试将数据存储在"addActivity"场景中的一张图:

export const m = new Map();

并在homeScene场景中导入贴图

import { m } from './addActivity';

我不确定这种方式是否好。我看到react-native-navigation有些函数可以传参,但是不知道怎么用。 现在最大的问题是当我从 "addActivity" 场景使用 goBack() 时如何刷新 "homeScene" 场景。 有两个场景的代码

homeScene.js

import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    TextInput,
    View,
    TouchableOpacity,
    Dimensions
} from 'react-native';

import { m } from './addActivity';


export default class LeftSideMenu extends Component {
    constructor(props) {
        super(props);

        this.state = {
            isOpen: false,
        }

    add = () => {
        const { navigate } = this.props.navigation;  
        navigate('Add');  
    }

render() {
        return (

                <View style={styles.container}>
                    //I want to show the data here
                    <Text
                        style={styles.btText2}>{m.get("title")}</Text>

                    //This is the bottom to jump to "addActivity" scene
                    <TouchableOpacity
                        onPress={this.add} 
                        style={styles.Addbutton}>
                        <Text
                            style={styles.btText2}>+</Text>
                    </TouchableOpacity>
                </View>

        );

    }
}

addActivity.js

import React, { Component } from 'react';
import { View, ScrollView, Dimensions, StyleSheet, Text, Button, TouchableOpacity, Alert } from 'react-native';

import { NavigationPage, ListRow, Input, Select, Label } from 'teaset';

let { height, width } = Dimensions.get('window');

//initial the map
export const m = new Map();

export default class InputExample extends Component {


  constructor(props) {
    super(props);
    this.state = {
      length: 0,
      title: '',
      isVisible: false,
    };
    Object.assign(this.state, {
      valueTitle: null,
    });
  }


//Return  to homeScene
  homeScene = () => {
    m.set("title", this.valueTitle);
    const { goBack } = this.props.navigation; 
    Alert.alert("Add succeed","return to Homepage",[{text: 'confirm', onPress: () => { goBack(); }}]) 

  }

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

        <ListRow title='Title *' detail={
          <Input
            style={{ width: 200 }}
            size='md'
            value={this.state.valueMD}
            placeholder=''
            onChangeText={text => this.setState({ valueTitle: text })}
          />
        } topSeparator='full' />

        <TouchableOpacity
          onPress={this.homeScene} 
          style={styles.button}>
          <Text
            style={styles.btText}>Confirm</Text>
        </TouchableOpacity>

      </ScrollView>
    );
  }
}

我希望当我对 homeScene 使用 goBack() 时,场景可以刷新并且来自 addActivity 场景的数据可以打印在 homeScene 上

为了在返回时刷新上一个屏幕,您可以在要在从某个屏幕返回时更新组件状态的屏幕中使用此代码

componentDidMount()
     this.getMoviesFromApiAsync()
     this.props.navigation.addListener('willFocus',this._handleStateChange);

    }

   _handleStateChange = state => {
   alert('Refreshed successfully!')
   this.getMoviesFromApiAsync()
 };

其中 getMoviesFromApiAsync() 是包含 API 调用代码的方法。

您可以使用 Redux 来实现相同的目的。