Skip to content
This repository has been archived by the owner on Nov 7, 2023. It is now read-only.

RNES on RN Web #69

Open
Tobinho07 opened this issue Dec 26, 2021 · 1 comment
Open

RNES on RN Web #69

Tobinho07 opened this issue Dec 26, 2021 · 1 comment

Comments

@Tobinho07
Copy link

I am using RNES for the first time. I know that secure storage is only available on Android and iOS but I am also testing with react native web. On executing on web I'm getting an error because of RNES.

My question : what is the best way to use it with web without to duplicate every screen with homescreen.web.js without importing RNES?

@emeraldsanto
Copy link
Owner

emeraldsanto commented Dec 31, 2021

I suppose you could create two files with platform specific extensions since both Metro and Webpack support them:

// storage.native.js

export * from 'react-native-encrypted-storage';

and

// storage.web.js

export * from '@react-native-async-storage/async-storage';

And then in your code

// some-other-file.js
import Storage from 'path/to/storage'; // don't include `.native.js` or `.web.js` in the import

Storage.setItem('key', 'value');

The bundler will load the correct file based on the platform it is running on. This assumes the other solution you're using has a similar API to this package (i.e. a default export with setItem, getItem, etc.).

You could also set a path alias in your Webpack configuration to resolve 'react-native-encrypted-storage' to something like '@react-native-async-storage/async-storage' which would require no code change on your end but is a bit harder to discover/maintain.

// webpack.config.js
module.exports = {
  // ...the rest of your config

  resolve: {
    alias: {
      'react-native-encrypted-storage': '@react-native-async-storage/async-storage'
    }
  }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants