Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blur event is not fired #6716

Closed
leethree opened this issue Sep 23, 2019 · 7 comments
Closed

Blur event is not fired #6716

leethree opened this issue Sep 23, 2019 · 7 comments

Comments

@leethree
Copy link
Contributor

Current Behavior

  • In stack navigator, blur event is not fired when navigating back to previous screen.

Expected Behavior

  • blur should be fired when going back.

How to reproduce

class SomeScreen extends React.Component {
  unsubscribeListener: () => void;

  componentDidMount() {
    this.unsubscribeListener = this.props.navigation.addListener(
      'blur',
      () => { console.log('blur'); },
    );
  }

  componentWillUnmount() {
    this.unsubscribeListener();
  }

  render() {
    // omitted
  }
}

Your Environment

software version
react-navigation 5.0.0-alpha.8
react-native 0.59.10
expo N/A
node 10.15.0
npm or yarn yarn 1.17.3
@leethree
Copy link
Contributor Author

Same thing happen after I upgraded to the latest versions

    "@react-navigation/bottom-tabs": "5.0.0-alpha.9",
    "@react-navigation/core": "5.0.0-alpha.10",
    "@react-navigation/native": "5.0.0-alpha.8",
    "@react-navigation/stack": "5.0.0-alpha.19",

@satya164
Copy link
Member

satya164 commented Sep 24, 2019

This is kind of expected behaviour that blur event doesn't fire for a screen that is getting removed. I'll try to make it work, but ideally whatever you were doing in blur, you should do in componentWillUnmount as well (the useFocusEffect hook handles it), so it's not very critical.

@leethree
Copy link
Contributor Author

I'm trying to prevent the back behaviour when the form on the screen is not saved.
componentWillUnmount won't work in this case.

I'll try convert it to functional component and use useFocusEffect.

@satya164
Copy link
Member

satya164 commented Sep 24, 2019

I'm trying to prevent the back behaviour when the form on the screen is not saved.

Don't think subscribing to blur event is correct here? You cannot prevent focus/blur events. You'll need to manually disable the hardware back button (by subscribing to BackHandler in useFocusEffect) and the back button on the toolbar.

@leethree
Copy link
Contributor Author

I thought I could call preventDefault on the blur event, which would be very handy in many use cases. I guess it's not possible 🤷‍♂

@Nerogee
Copy link

Nerogee commented Jan 14, 2020

Hi Guys,
any updates? the feature @leethree looked after is mentioned here
https://reactnavigation.org/docs/en/next/navigation-prop.html#navigation-events

but I'm failed to implemented. May I know useFocusEffect is the key to this? Because I used componentDidMount to addListener for 'blur' event, but it's an error saying invalid event.

@leethree
Copy link
Contributor Author

@Nerogee You won't be able to prevent the screen from going back using blur event. We end up implementing a custom logic to do that.

@satya164 satya164 transferred this issue from react-navigation/navigation-ex Feb 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants