Pasindu Weerakoon
Pasindu Weerakoon

Reputation: 628

How to use singleton pattern in React Native

I'm converting an Android application to React Native and I'm new to this react-native technology.

In my android application, I have used the Singleton object to store some data. I just want to create the same for the react-native application. Inside the Java singleton object, it has a list eg : public List<PharmacyLocationChecklistsDTO> pharmacyLocationChecklistsDTOS;.

Scenario: In my java application has wizard this singleton object store all the data of that wizard view. I have already implemented the wizard in my react native application, but I don't know how to store data until the user completes the task.

How could I create this object in my react-native app or is there any other way to handle this scenario?

Response,

"pharmacyLocationChecklistsDTOS": [
            {
                "chelistid": 232,
                "ireqstId": 5,
                "checklistName": "E",
                "validity": "Invalid",
                "remark": null
            }
  ]

Sample Java Object,

public class PharmacyLocationChecklistsDTO{
    public int chelistid;
    public int ireqstId;
    public String checklistName;
    public String validity;
    public Object remark;
}

public class Root{
    public List<PharmacyLocationChecklistsDTO> pharmacyLocationChecklistsDTOS;
}

Upvotes: 3

Views: 804

Answers (1)

safaiyeh
safaiyeh

Reputation: 1715

This is the job of a state management system.

There are popular options like Redux, MobX, React's Context API, Recoil. They will allow multiple components to consume the same state.

I'd suggest to go through them and see which caters to your needs the most.

Upvotes: 1

Related Questions