为了账号安全,请及时绑定邮箱和手机立即绑定

React Native中导航栈显示屏幕时调用哪个方法

React Native中导航栈显示屏幕时调用哪个方法

LEATH 2023-07-06 17:13:50
我是 React Native 的新手,我正在使用import {  createAppContainer } from 'react-navigation'; import { createStackNavigator } from 'react-navigation-stack'用于从一个屏幕导航到下一个屏幕,并在按后退按钮时显示堆栈中的屏幕。我想在屏幕从堆栈显示时聚焦 TextInput。FirstScreen -> SecondScreen (Back Button Press)-> FirstScreen (Focus TextInput)当FirstScreen从堆栈中显示时,会调用哪个方法?我尝试将代码放入ComponentDidMonunt(), ComponentWillMount(), render(), ComponentWillUnmount()方法中,但当从堆栈中显示 FirstScreen 时,不会调用这些方法。
查看完整描述

1 回答

?
慕慕森

TA贡献1856条经验 获得超17个赞

尝试在react-navigation中使用NavigationEvent 

class Profile extends React.Component {

  componentDidMount() {

    this._unsubscribe = navigation.addListener('focus', () => {

      // do something

      this.emailInput.focus()

    });

  }


  componentWillUnmount() {

    this._unsubscribe();

  }


  render() {

      <View>

        <TextInput ref={ref => { this.emailInput = ref; }}/>

      <View/>

  }

}


查看完整回答
反对 回复 2023-07-06
  • 1 回答
  • 0 关注
  • 74 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信