How to migrate from v3 to v4

Requirements

  • React Native 0.85+ with the New Architecture enabled (previously 0.72)
  • iOS 15.1+ (previously iOS 11+)
  • Android minSdkVersion 24+

Step 1: Update your dependencies

Remove the v3 package, @datadome/datadome-axios:

npm uninstall @datadome/datadome-axios

Install the v4 package and the CoreDataDome SDK:

npm install @datadome/react-native-datadome-axios @datadome/react-native-core-datadome

The v4 SDK does not use react-native-webview. If your application does not use it directly, remove it:

npm uninstall react-native-webview

On iOS, install the pods:

cd ios && pod install

Step 2: Create a DataDome instance

Replace the configure call with a DataDome instance. Give the client-side key to the constructor in the clientKey property.

Before:

componentDidMount(): void {
    DataDome.configure("Client_Side_Key");
  }

After:

import { DataDome } from '@datadome/react-native-datadome-axios';

const datadome = new DataDome({ clientKey: 'YOUR_CLIENT_KEY' });

Step 3: Give the DataDome instance to the interceptor

The DataDomeInterceptor constructor now takes the DataDome instance as its parameter.

Before:

import { DataDomeInterceptor } from '@datadome/datadome-axios';

const interceptor = new DataDomeInterceptor();

Or, with a custom Axios instance:

const axiosInstance = axios.create({
  baseURL: 'https://your_domain.com/'
});

//initialize DataDome with custom axios instance
const interceptor = new DataDomeInterceptor({axiosInstance});

After:

import { DataDomeInterceptor } from '@datadome/react-native-datadome-axios';

const dataDomeInterceptor = new DataDomeInterceptor(datadome);

Step 4: Replace your interceptors with attach

You do not need to call these DataDomeInterceptor methods anymore:

  • requestAdapter, in a request interceptor. It added the DataDome cookie to each request.
  • responseEventHandler, in the response success handler. It sent mobile signals to DataDome.
  • responseAdapter, in the response error handler. It evaluated the response, showed the challenge, and retried the request.

The new attach method does all of these operations on your Axios instance. Remove your DataDome interceptors and call attach:

Before:

import axios from 'axios';

//set request intercepter
axios
  .interceptors
  .request
  .use(async function(request) {
    return await interceptor.requestAdapter(request)
   }, 
   function(error) {
      return Promise.reject(error);
   }
);

//set response interceptor
axios
  .interceptors
  .response
  .use(function (response) {
       //sending mobile signals
     interceptor.responseEventHandler(response);
     return response;
   },
   async function(error) {
      return await interceptor.responseAdapter(error)
   }
);

After:

dataDomeInterceptor.attach(axios);

Step 5: Remove DataDomeModal

Remove the DataDomeModal component from your views. The SDK now shows the challenges with native code.

Before:

import { DataDomeModal } from '@datadome/datadome-axios';

export default class MyApp extends Component {
  render() {
    return (
      <View style={{ flex: 1 }}>
        <DataDomeModal />
        <Text>Hello, world!</Text>
      </View>
    );
  }
}

After:

export default class MyApp extends Component {
  render() {
    return (
      <View style={{ flex: 1 }}>
        <Text>Hello, world!</Text>
      </View>
    );
  }
}

Did this page help you?