Skip to content

Instantly share code, notes, and snippets.

@agustinfece
Created June 24, 2021 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agustinfece/1510ac26321edfbdd2692c7d8eed06ec to your computer and use it in GitHub Desktop.
Save agustinfece/1510ac26321edfbdd2692c7d8eed06ec to your computer and use it in GitHub Desktop.
App.js
import React, {useState} from 'react';
import {View, TextInput, StyleSheet, NativeModules} from 'react-native';
import SharedGroupPreferences from 'react-native-shared-group-preferences';
const group = 'group.asap';
const SharedStorage = NativeModules.SharedStorage;
const App = () => {
const [text, setText] = useState('');
const widgetData = {
text,
};
const handleSubmit = async () => {
try {
// iOS
await SharedGroupPreferences.setItem('widgetKey', widgetData, group);
} catch (error) {
console.log({error});
}
// Android
SharedStorage.set(JSON.stringify({text}));
};
return (
<View style={styles.container}>
<TextInput
style={styles.input}
onChangeText={newText => setText(newText)}
value={text}
returnKeyType="send"
onEndEditing={handleSubmit}
placeholder="Enter the text to display..."
/>
</View>
);
};
export default App;
const styles = StyleSheet.create({
container: {
marginTop: '50%',
paddingHorizontal: 24,
},
input: {
width: '100%',
borderBottomWidth: 1,
fontSize: 20,
minHeight: 40,
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment