react-native-cookies
The @amazon-devices/react-native-cookies library provides a way for React Native applications to manage cookies.
A cookie is a small piece of data stored on the user's device by the web browser while browsing a website. Cookies are designed to be a reliable mechanism for websites to remember stateful information or to record the user's browsing activity.
Installation
-
Add the JavaScript library dependency in the
package.jsonfile:"dependencies": { ... "@amazon-devices/react-native-cookies__cookies": "1.0.1+6.2.1", "@react-native-cookies/cookies": "6.2.1", ... } -
Reinstall dependencies using the
npm installcommand.
Example
The following example shows all methods already implemented in this library.
import CookieManager, { Cookie } from "@react-native-cookies/cookies";
import React, { useState } from 'react';
import { Button, ScrollView, StyleSheet, Text, View } from 'react-native';
const App = () => {
const [response, setResponse] = useState('');
const mockedCookie: Cookie = {
name: 'myCookie',
value: 'myValue',
domain: 'httpbin.org',
path: '/',
httpOnly: true,
expires: '2030-06-01T12:30:00.00-05:00'
};
const url = "https://httpbin.org";
const testMockedFunction = async (func: (...args: any[]) => Promise<any>, ...args: any) => {
try {
setResponse("");
await func(...args).then(
(result : any) => setResponse(JSON.stringify(result)),
(reject : any) => setResponse(JSON.stringify(reject))
);
} catch(err) {
if (err instanceof Error) {
setResponse(err.message);
}
}
};
const fetchTest = async () => {
setResponse("");
const result = await fetch("https://httpbin.org/get");
setResponse(`${await result.text()}`);
};
return (
<View style={styles.container}>
<View style={styles.textArea}>
<ScrollView>
<Text style={styles.text} numberOfLines={999}>
{response}
</Text>
</ScrollView>
</View>
<View style={[styles.buttonRow, { width: 800 }]}>
<Button title ={"Fetch from httpbin.org"} onPress={() => fetchTest()} />
<Button title={"Test set method"} onPress={() => testMockedFunction(CookieManager.set, url, mockedCookie)} />
<Button title={"Get httpbin.org Cookies"} onPress={() => testMockedFunction(CookieManager.get, url)} />
<Button title={"Clear All Cookies"} onPress={() => testMockedFunction(CookieManager.clearAll)} />
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
backgroundColor: '#dfebeb',
justifyContent: 'center',
paddingHorizontal: 5,
flex: 1,
display: 'flex',
alignItems: 'center',
overflow: 'hidden',
},
textArea: {
width: 750,
height: 250,
alignItems: 'center',
marginVertical: 20
},
buttonRow: {
flexDirection: 'row',
marginVertical: 10,
justifyContent: 'space-between'
},
text: {
color: 'black',
fontFamily: 'monospace',
overflow: 'scroll',
flex: 1,
}
});
export default App;
API reference
Check out the official README from @react-native-cookies/cookies's GitHub repository.
The list below shows the current state of implemented functionalities.
| Method | Description |
|---|---|
set |
Set a cookie given a Cookie object and a URL |
get |
Get cookies for a URL |
clearAll |
Clear cookies |
- When no
domainfield is specified for the Cookie object, a URL host is used instead. - When no
pathis specified for the Cookie object, an empty path/is assumed. - Setting
useWebKit = trueon the Vega platform, for methods that accept the parameter, is a no-op. If no value is provided,useWebKitdefaults tofalse. - Passing a malformed URL, to any methods that accept a URL, returns an error message.
Implementation details
- The accepted date-time format for the
expiresfield of a Cookie object is the ISO8601 format with an offset from UTC. After a cookie is set on Vega, the date-time is always converted to GMT.- For example, after
mockedCookiefrom the previous example is set, subsequent retrievals return a timestamp ofexpires: '2030-06-01T17:30:00.00Z'. This timestamp is the equivalent time in GMT.
- For example, after
- When session cookies (cookies with no
expiresfield) are retrieved withget, theexpiresfield is populated with1970-01-01T00:00:00Z. If this cookie is used with other Cookie Managers, remove the field from the cookie. Otherwise, the cookie is read as expired by other Cookie Manager implementations (such as Webview Cookie Manager).
Supported versions
| Package name | Amazon NPM library version | Vega OS build number | Vega SDK version | Release notes |
|---|---|---|---|---|
@amazon-devices/react-native-cookies__cookies |
1.0.1+6.2.1 | OS 1.1 (201010438050) |
0.20 |
Additional resources
For information on other libraries, see Supported Third-Party Libraries and Services.
Last updated: Oct 02, 2025

