0

Hi all so currently I am making a service for my application/library i am creating and so far this is the best way I can figure out how to create a service. Ideally I would like to change this method into a variable for easier reading

import { NavigationRoute, NavigationParams } from 'react-navigation';
import { NavigationStackProp } from 'react-navigation-stack';

type Navigation =
  | NavigationStackProp<NavigationRoute<NavigationParams>, NavigationParams>
  | undefined;

export default class NavigationService {
  private static _navigator: Navigation;

  public static setNavigator(navigatorRef: Navigation) {
    this._navigator = navigatorRef;
  }

  public static navigate = (): Navigation => {
    // TODO: look into how to make this a variable
    return NavigationService._navigator;
  };
}

currently i have this

      IconPress: () => NavigationService.navigate()?.openDrawer()

but would love for it to read like this

      IconPress: () => NavigationService.navigate?.openDrawer()

1 Answer 1

1

This is called a getter:

export default class NavigationService {

    public static get navigate(): Navigation {
        return NavigationService._navigator;
    } 
}
Sign up to request clarification or add additional context in comments.

1 Comment

amazing thank you. wasn't sure how to write the correct syntax :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.