Skip to content
Advertisement

React component not rerendering after state change despite using setState()

I tried to fetch data from an embedded sqlite db and displayed it in list format. The fetchData() function is working, with correct returned result. However, when I used setState() to update the state, react did not re-render. Next I tried to setState() with array [‘a’,’b’,’c’] at componentDidMount(). React did re-render and showed a, b, c.

JavaScript

Advertisement

Answer

Because of db.each won’t return a Promise so that you cannot reach result by ‘await’ statement. Refer to the APIs, you’ll need to this:

JavaScript

so, you should access the result in the complete callback, code like so:

JavaScript

Hope it will be helpful.


Besides, you can also have a try with Statement#all([param, ...], [callback]) function which is a better than each.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement