React native- Best way to create singleton pattern

前端 未结 3 1962
滥情空心
滥情空心 2020-12-31 10:32

I am new in react-native coding but have experienced on objective-c and swift coding and want use singleton pattern in react-native. I have tried to find out the solution fr

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-31 10:42

    Here's my implementation for singleton class...

    Controller.js

    export default class Controller {
        static instance = Controller.instance || new Controller()
    
        helloWorld() {
            console.log("Hello World... \(^_^)/ !!")
        }
    }
    

    Usage:

    import Controller from 'Controller.js'
    
    Controller.instance.helloWorld()
    

提交回复
热议问题