最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Which lifecycle event is called when a screen appears? - Stack Overflow

programmeradmin4浏览0评论

Suppose I have two screens:

  • Screen A
  • Screen B

I am initially landed on Screen A. When I click on a Button I navigate to Screen B. When I press Back Button, I am again navigated to Screen A.

I want to call an action creator when I am navigated to Screen A as mentioned in above scenario.

I just want to know that which lifecycle event will be called every time when a screen is presented.

Isn't there some event like ponentWillAppear()?

Note: I am using react-native with react-navigation for navigation.

Suppose I have two screens:

  • Screen A
  • Screen B

I am initially landed on Screen A. When I click on a Button I navigate to Screen B. When I press Back Button, I am again navigated to Screen A.

I want to call an action creator when I am navigated to Screen A as mentioned in above scenario.

I just want to know that which lifecycle event will be called every time when a screen is presented.

Isn't there some event like ponentWillAppear()?

Note: I am using react-native with react-navigation for navigation.

Share Improve this question edited Dec 4, 2017 at 6:39 Val 22.8k11 gold badges71 silver badges87 bronze badges asked Dec 4, 2017 at 6:29 VishalVishal 6,37814 gold badges89 silver badges164 bronze badges 7
  • which lifecycle event will be called every time when a screen is presented render. But you can use ponentDidMount – Rajesh Commented Dec 4, 2017 at 6:31
  • @Rajesh Are you sure that ponentDidMount will always be called when I goBack to view? – Vishal Commented Dec 4, 2017 at 6:32
  • If the ponent is rendered again, yes. If you are using css to show/hide, then not sure. – Rajesh Commented Dec 4, 2017 at 6:33
  • @Rajesh I am not using css but I use react-navigation to navigate between the screens – Vishal Commented Dec 4, 2017 at 6:34
  • Just check, on back button, if the screenA's render is called ot not. If not, you might have to pass a callback for back button. – Rajesh Commented Dec 4, 2017 at 6:35
 |  Show 2 more ments

3 Answers 3

Reset to default 13

This can now be done with plain react navigation via their listeners:

In your Component A:

ponentDidMount = () => {
  this._ponentFocused();

  this._sub = this.props.navigation.addListener(
    'didFocus',
    this._ponentFocused
  );
}

ponentWillUnmount() {
  this._sub.remove();
}

_ponentFocused = () => {
  this.setState({dataToShow});
}

React Navigation docs - Lifecycle

When you navigate from one screen to another, the first screen will not be unmounted, but still on the stack, just hide in the background.

What you need might be ponentDidFocus, but it's currently in design not avaiable yet, see react-native open issue #51.

You can try this alternative way: react-navigation-addons. With this you can have events focus & blur which may suit your needs.

If you use react-native-navigation, you can listen for appearance events: https://wix.github.io/react-native-navigation/#/screen-api?id=listen-to-visibility-events-globally

发布评论

评论列表(0)

  1. 暂无评论