Deep linking is not working when app is closed/killed
  • 30-Apr-2023
Lightrun Team
Author Lightrun Team
Share
Deep linking is not working when app is closed/killed

Deep linking is not working when app is closed/killed

Lightrun Team
Lightrun Team
30-Apr-2023

Explanation of the problem

When the app is in the background, a specific screen is opened as expected through a deep link. However, when the app is closed or not in the background, it only shows the first screen through the deep link. Various workarounds found on Stack Overflow were tried, but none of them resolved the issue. The app is built using React Native version 0.64.2.

Code:

The linking.js file contains a config object that defines two screens, Home and Profile. The prefixes property defines the URL scheme used for the app, which is demo://app. This file exports the linking object.

The App.js file contains the main component that uses NavigationContainer from @react-navigation/native. It also imports the linking object and defines a Stack navigator that has three screens: SplashScreen, Home, and Profile. The initial route is set to SplashScreen, and the screenOptions are set to use the slide from right iOS transition preset. The Home and Profile screens are not enabled for gestures and do not show the header.

The _handleOpenUrl function is used to handle opening a URL in the app. It logs the event URL to the console. The Linking.getInitialURL() method is called to check if the app was launched through a deep link. If a URL is found, the _handleOpenUrl function is called with the URL. The useEffect hook is used to add an event listener for the url event and remove it on unmount.

 

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for Deep linking is not working when app is closed/killed

When developing mobile applications that support deep linking, it’s essential to have the correct configuration for handling them. In both solutions, we can see that the linking configuration is defined and used to configure the NavigationContainer component. The first solution also uses Expo-Linking to create a prefix for deep links, which is added to the linking configuration. This ensures that when the user clicks on a deep link, the correct screen is displayed in the application. Additionally, the first solution defines a variable for the deep link URL and checks if it’s defined before opening it. By using the Linking module, we can ensure that the application handles deep links correctly.

To handle deep links properly, it’s also necessary to use the Linking module. Both solutions use the Linking module to check if the app was opened from a deep link and get the initial URL. The first solution uses the getInitialURL() method to check if the app was opened from a deep link and stores the URL in a variable. This variable is then used to open the deep link when the NavigationContainer is ready. Similarly, the second solution uses the getInitialURL() method to get the initial URL and return it. By using the Linking module to handle deep links, we can ensure that our application behaves consistently and as expected.

Finally, the NavigationContainer onReady function is used to handle the opening of deep links. The first solution defines an onNavigationReady() function that checks if a deep link URL is defined and opens it if it is. This function is passed to the onReady prop of the NavigationContainer component. Similarly, the second solution doesn’t use onNavigationReady(), but instead returns the initial URL from getInitialURL(). By using the onReady prop, we can ensure that the application is fully loaded and ready to handle deep links before attempting to open them.

In conclusion, to handle deep links in React Native applications, it’s necessary to have the correct configuration for deep links, use the Linking module to handle them, and use the NavigationContainer onReady function to handle the opening of deep links. By following these best practices, we can ensure that our application behaves consistently and as expected when handling deep links.

 

Other popular problems with React

Problem: Virtual DOM performance issues

One of the most common problems with React is related to performance issues with the virtual DOM. The virtual DOM is a mechanism that React uses to update the view in response to changes in the underlying data. When the state of a component changes, React will first update the virtual DOM, and then update the actual DOM. This process can be slow, particularly if the component has a large number of child elements.

Solution:

To solve this problem, developers can use techniques such as shouldComponentUpdate, which allows components to control when they should re-render, or use the React.memo higher-order component, which only re-renders a component when its props change. Additionally, developers can use the React DevTools extension to identify and optimize components that are causing performance bottlenecks.

Problem: Managing state and props

Another common problem with React is related to managing state and props. React components can have both state and props, which are used to store and pass data between components. However, when a large number of components need to share and update data, it can become difficult to manage and maintain the flow of data.

Solution:

To solve this problem, developers can use a centralized state management library such as Redux or MobX. These libraries allow developers to store all of the application’s state in a single place, and provide a mechanism for updating and sharing that state across all components. Additionally, developers can use the useContext and useReducer hooks to manage state within a component tree.

Problem: Handling Forms

React forms can also be a source of pain for developers, as they require a lot of boilerplate code to handle changes and validation. Forms can be tricky to handle because they often involve multiple inputs, which need to be controlled and updated correctly.

Solution:

To solve this problem, developers can use libraries such as Formik and react-hook-form to handle forms in React. These libraries provide simple APIs for controlling form inputs and handling validation. Additionally, developers can use the useState and useEffect hooks to handle form input changes and validation manually.

A brief introduction to React

React is a JavaScript library for building user interfaces. It was developed by Facebook and is now maintained by a community of developers. React allows developers to build reusable UI components, which can be composed to create complex user interfaces. React uses a virtual DOM (Document Object Model) to improve performance by limiting the amount of changes that need to be made to the actual DOM. The virtual DOM is a lightweight representation of the actual DOM and it allows React to compare the current state of the virtual DOM with the previous state, and make only the necessary changes to the actual DOM.

React follows a component-based architecture, where the user interface is broken down into small, self-contained components that can be easily reused and composed to create more complex UI. React components can have both state and props, which are used to store and pass data between components. React also provides a mechanism for handling events, such as user clicks, through the use of event handlers. React also provides a set of lifecycle methods that developers can use to control when a component is created, updated, and destroyed. This allows developers to control the behavior of their components and optimize performance.

Most popular use cases for React

  1. Building reusable UI components React allows developers to build reusable UI components that can be composed to create complex user interfaces. Each component is self-contained and can manage its own state and props. This allows developers to easily reuse and maintain their code.
  2. Dynamic User Interfaces React uses a virtual DOM to improve performance by limiting the amount of changes that need to be made to the actual DOM. This allows developers to build dynamic user interfaces that can efficiently update in response to changes in the underlying data.
  3. Building complex web applications React can be used to build complex web applications that require efficient updating of dynamic data. React also provides a set of lifecycle methods that developers can use to control when a component is created, updated, and destroyed. This allows developers to control the behavior of their components and optimize performance.
class Counter extends React.Component {
  constructor(props) {
    super(props);
    this.state = { count: 0 };
  }

  handleClick = () => {
    this.setState({ count: this.state.count + 1 });
  }

  render() {
    return (
      <div>
        <p>Count: {this.state.count}</p>
        <button onClick={this.handleClick}>
          Increment
        </button>
      </div>
    );
  }
}
Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.