keil
keil

Reputation: 39

turn static method of class into variable typescript

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()

Upvotes: 0

Views: 35

Answers (1)

Rubydesic
Rubydesic

Reputation: 3476

This is called a getter:

export default class NavigationService {

    public static get navigate(): Navigation {
        return NavigationService._navigator;
    } 
}

Upvotes: 1

Related Questions