Skip to main content

React Native Integration

GUN works with React Native, but requires additional setup for crypto polyfills and platform-specific storage adapters.

Installation

1

Install GUN and dependencies

Install GUN and required shims for React Native:
2

Create shim file

Create a shim.js file in your project root to polyfill Node.js globals:
3

Import shim in index.js

Import the shim at the very top of your index.js:
4

Set up crypto polyfill

Create a crypto polyfill component for WebCrypto API support.

WebCrypto Polyfill

GUN’s SEA (Security, Encryption, Authorization) requires the WebCrypto API. Here’s how to polyfill it:

AsyncStorage Adapter

For persistent storage in React Native, create an AsyncStorage adapter:

Complete Setup Example

Here’s a complete React Native app with GUN integration:

App.js

Demo Component with GUN

Platform-Specific Considerations

iOS

  • WebCrypto polyfill is required for SEA functionality
  • AsyncStorage is deprecated - consider using @react-native-async-storage/async-storage

Android

  • Ensure network permissions are set in AndroidManifest.xml
  • Test with release builds as debug mode may have different performance characteristics

Common Issues

Buffer/Crypto Errors

Make sure shims are imported before any GUN imports:

Storage Not Persisting

Verify that the AsyncStorage adapter is registered before creating GUN instances.

Best Practices

  1. Load polyfills early: Import shims at the app entry point
  2. Use AsyncStorage adapter: For offline-first functionality
  3. Test on devices: Simulators may not accurately represent performance
  4. Handle permissions: Request necessary permissions for network access

Next Steps