mardi 8 mars 2016

Rendering data returned from Sqlite with React and Phonegap

I'm working on a PhoneGap app using React and SQLite locally and Parse in the cloud.

The problem I'm running into is rendering the results of the local DB query on the screen.

The root cause seems to be the fact that it is not possible to set the React component's state or locally declared variable from within the DB Success Callback.

Is there a different way to achieve this?

Here's the code, which is a part of my component's "success:" event. The problem is that sortedContacts set in the callback doesn't update the variable, and it stays with it's original value. Tried to set state - same result.

        getContactsFromDB();

        this.setState({contactsStatus: "",
                           renderedContacts: sortedContacts.sort()});

        function getContactsFromDB(){

              dbobj.transaction(selectRecords);

              function selectRecords(tx)
              {
                  tx.executeSql('SELECT Name FROM CONTACTS WHERE UserName = ?', [userName], successResults);
              }

              function successResults(tx,results){
                    var len = results.rows.length;

                    for (var i=0; i<len; i++){

                        sortedContacts.push(results.rows.item(i).Name);

                      }
                    }

        }

Aucun commentaire:

Enregistrer un commentaire