Preload JSON files

#1

I’m trying to preload some JSON files before the app starts, but I keep getting this error:

[Unhandled Promise rejection: TypeError: undefined is not an object (evaluating 'meta.moduleId = moduleId')]

My code is as follows:

await Asset.loadAsync([
        require('./assets/i18n/en-gb.json'),
        require('./assets/i18n/da.json'),
        require('./assets/i18n/de.json'),
      ])

Could it be that JSON files are not allowed?

Thanks in advance.

0 Likes

#2

JSON files are automatically parsed into an object – they cannot be used as assets for this reason. If you really want to use as plain text then you could change the extension to .txt. Otherwise if you do const obj = require('blah.json') then obj will already contain the keys of the JSON file like obj.foo and obj.bar if blah.json's contents are { foo: ..., bar: ..., ... }.

1 Like

Caching JSON resources with expo
#3

Thanks for clarifying that! I just discovered the File System API, and I’ll try to download my assets and cache them locally.

0 Likes