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.json
file:"dependencies": { ... "@amazon-devices/react-native-cookies__cookies": "1.0.1+6.2.1", "@react-native-cookies/cookies": "6.2.1", ... }
- Reinstall dependencies using
npm install
command.
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 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
domain
field is specified for the Cookie object, url host will be used instead. - When no
path
is specified for the Cookie object, an empty path/
will be assumed. - Setting
useWebKit = true
on Vega platform for any of the methods which accept the parameter is a no-op.useWebKit
defaults tofalse
if no value is provided. - Passing a malformed URL to any of the methods which accept a URL will return an error message.
Implementation details
- The accepted date-time format for the
expires
field of a Cookie object is ISO8601 format with offset from UTC. On Vega, after the cookie is set this will always be converted to GMT.- For example, after
mockedCookie
from the above example is set, subsequent retrievals will return a timestamp ofexpires: '2030-06-01T17:30:00.00Z'
which is the equivalent time in GMT.
- For example, after
- When session cookies (cookies with no
expires
field) are retrieved withget
, theexpires
field is populated with1970-01-01T00:00:00Z
. If this cookie is to be used with other Cookie Managers, you may need to remove the field from the cookie or else it could be considered to be already 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 additional libraries, see Supported Third-Party Libraries and Services.
Last updated: Oct 02, 2025