I have the below togglebutton and I have 2 of them, I want them to work like this:…
- User
I have the below togglebutton and I have 2 of them, I want them to work like this: Firstly, if I navigate to the page, one of the toggle button's should already be pressed. Secondly, if I press the other toggle button, the first one should untoggle.
import React, { useState } from "react" import { TouchableOpacity, Image} from 'react-native'
import styles from './ToggleBtn.style'
const ToggleBtn = (props) => { const [isToggled, setIsToggled] = useState(false); const toggleHandler = () => setIsToggled(previousState => !previousState);
return( <TouchableOpacity style={isToggled ? styles.toggleBtnContainerOn : styles.toggleBtnContainer} onPress={toggleHandler} value={isToggled}> ) }
export default ToggleBtn
- Assistant
In order to achieve the desired behavior, you need to lift the state up to the parent component and pass the toggle state and handler as props to the ToggleBtn components. Here's a way to implement it:
- Create a new parent component that will hold the state and the toggle logic:
import React, { useState } from "react"; import ToggleBtn from "./ToggleBtn"; // import the ToggleBtn component const ToggleBtnGroup = () => { const [isToggled, setIsToggled] = useState(0); const toggleHandler = (index) => { setIsToggled(index); }; return ( <React.Fragment> <ToggleBtn imgUrl={/* image url for the first button */} toggled={isToggled === 0} onPress={() => toggleHandler(0)} /> <ToggleBtn imgUrl={/* image url for the second button */} toggled={isToggled === 1} onPress={() => toggleHandler(1)} /> </React.Fragment> ); }; export default ToggleBtnGroup;- Update the ToggleBtn component to accept the new props:
import React from "react"; import { TouchableOpacity, Image } from "react-native"; import styles from "./ToggleBtn.style"; const ToggleBtn = ({ imgUrl, toggled, onPress }) => { return ( <TouchableOpacity style={ toggled ? styles.toggleBtnContainerOn : styles.toggleBtnContainer } onPress={onPress} value={toggled} > <Image style={styles.toggleBtnImg} source={imgUrl} /> </TouchableOpacity> ); }; export default ToggleBtn;Now, when you use the ToggleBtnGroup component, it will render two ToggleBtn components and handle the state and toggling logic correctly:
import React from "react"; import { View } from "react-native"; import ToggleBtnGroup from "./ToggleBtnGroup"; const MyApp = () => { return ( <View> <ToggleBtnGroup /> </View> ); }; export default MyApp; - User
I have the below togglebutton and I have 2 of them, I want them to work like this: Firstly, if I navigate to the page, one of the toggle button’s should already be pressed. Secondly, if I press the other toggle button, the first one should untoggle.
import React, { useState } from “react” import { TouchableOpacity, Image} from ‘react-native’
import styles from ‘./ToggleBtn.style’
const ToggleBtn = (props) => { const [isToggled, setIsToggled] = useState(false); const toggleHandler = () => setIsToggled(previousState => !previousState);
return( <TouchableOpacity style={isToggled ? styles.toggleBtnContainerOn : styles.toggleBtnContainer} onPress={toggleHandler} value={isToggled}> ) }
export default ToggleBtn
import React, { useState, useLayoutEffect } from 'react' import { View, Text, SafeAreaView, TouchableOpacity, Switch } from 'react-native'
import styles from './TesztValaszto.style' import { COLORS } from "../../constants";
import magyarazattalImg from '../../assets/images/magyarazattal_2.png' import magyarazatNelkulImg from '../../assets/images/magyarazat_nelkul_2.png' import kozepImg from '../../assets/images/kozep.png' import emeltImg from '../../assets/images/emelt.png'
import ToggleBtn from './ToggleBtn' import Picker from './Picker'
const TesztValaszto = ({navigation, route}) => {
const [isEnabled, setIsEnabled] = useState(false); const toggleSwitch = () => setIsEnabled(previousState => !previousState);
const { tantargy } = route.params;
useLayoutEffect(() => { navigation.setOptions({ title: tantargy }); }, [navigation, tantargy]);
return ( <View style={[styles.toggleBtnContainer, styles.left]}> Közép <View style={[styles.toggleBtnContainer, styles.right]}> Emelt
<View style={styles.row2}> <View style={styles.evszamPickerContainer}> <Picker majdEzKiLeszValasztvaOmegaLUL='2022. Október'/> <Picker majdEzKiLeszValasztvaOmegaLUL='2005. Október'/> </View> <View style={styles.egyEvszamContainer}> <Text style={styles.switchDescription}>Csak egy évszámot szeretnék</Text> <Switch trackColor={{false: '#767577', true: COLORS.accentBlue}} thumbColor={isEnabled ? COLORS.primary : '#f4f3f4'} ios_backgroundColor="#3e3e3e" onValueChange={toggleSwitch} value={isEnabled} /> </View> </View> <View style={styles.row1}> <View style={[styles.toggleBtnContainer, styles.left]}> <View style={styles.toggleBtnDescriptionContainer}> <Text style={styles.toggleBtnDescription}>Magyarázattal</Text> </View> <ToggleBtn imgUrl={magyarazattalImg} name='toggle3'/> </View> <View style={[styles.toggleBtnContainer, styles.right]}> <View style={styles.toggleBtnDescriptionContainer}> <Text style={styles.toggleBtnDescription}>Magyarázat Nélkül</Text> </View> <ToggleBtn imgUrl={magyarazatNelkulImg} name='toggle4'/> </View> </View> <View style={styles.row4}> <TouchableOpacity style={styles.mehetBtn}> <Text style={styles.mehetBtnText}>Mehet</Text> </TouchableOpacity> </View> </SafeAreaView>) }
export default TesztValaszto
- Assistant
First, create a new parent component that will hold the state and toggle logic for each pair of toggle buttons, as mentioned in the previous answer:
import React, { useState } from "react"; import ToggleBtn from "./ToggleBtn"; const ToggleBtnGroup = ({ images, onToggle }) => { const [isToggled, setIsToggled] = useState(0); const toggleHandler = (index) => { setIsToggled(index); onToggle(index); }; return ( <> <ToggleBtn imgUrl={images[0]} toggled={isToggled === 0} onPress={() => toggleHandler(0)} /> <ToggleBtn imgUrl={images[1]} toggled={isToggled === 1} onPress={() => toggleHandler(1)} /> </> ); }; export default ToggleBtnGroup;Then, update the TesztValaszto component to use the new ToggleBtnGroup component for each pair of toggle buttons:
// ... previous imports ... import ToggleBtnGroup from "./ToggleBtnGroup"; // Import the new ToggleBtnGroup component const TesztValaszto = ({ navigation, route }) => { // ... previous useState and useLayoutEffect hooks ... // New state and handler to store the toggle state of both pairs of toggle buttons const [toggleState1, setToggleState1] = useState(0); const [toggleState2, setToggleState2] = useState(0); return ( <SafeAreaView style={styles.screen}> {/* ... row1 */} <View style={styles.row1}> <View style={[styles.toggleBtnContainer, styles.left]}> <View style={styles.toggleBtnDescriptionContainer}> <Text style={styles.toggleBtnDescription}>Közép</Text> </View> <ToggleBtnGroup images={[kozepImg, emeltImg]} onToggle={(index) => setToggleState1(index)} /> </View> {/* ... */} </View> {/* ... row2 ... */} {/* ... row3 */} <View style={styles.row2}> <View style={[styles.toggleBtnContainer, styles.left]}> <View style={styles.toggleBtnDescriptionContainer}> <Text style={styles.toggleBtnDescription}>Magyarázattal</Text> </View> <ToggleBtnGroup images={[magyarazattalImg, magyarazatNelkulImg]} onToggle={(index) => setToggleState2(index)} /> </View> {/* ... */} </View> {/* ... row4 ... */} </SafeAreaView> ); }; export default TesztValaszto;Now, when you use the ToggleBtnGroup component in the TesztValaszto component, it will render each pair of toggle buttons and handle the state and toggling logic correctly.
- User
I have the below togglebutton and I have 2 of them, I want them to work like this: Firstly, if I navigate to the page, one of the toggle button’s should already be pressed. Secondly, if I press the other toggle button, the first one should untoggle.
import React, { useState } from “react” import { TouchableOpacity, Image} from ‘react-native’
import styles from ‘./ToggleBtn.style’
const ToggleBtn = (props) => { const [isToggled, setIsToggled] = useState(false); const toggleHandler = () => setIsToggled(previousState => !previousState);
return( <TouchableOpacity style={isToggled ? styles.toggleBtnContainerOn : styles.toggleBtnContainer} onPress={toggleHandler} value={isToggled}> ) }
export default ToggleBtn
import React, { useState, useLayoutEffect } from ‘react’ import { View, Text, SafeAreaView, TouchableOpacity, Switch } from ‘react-native’
import styles from ‘./TesztValaszto.style’ import { COLORS } from “…/…/constants”;
import magyarazattalImg from ‘…/…/assets/images/magyarazattal_2.png’ import magyarazatNelkulImg from ‘…/…/assets/images/magyarazat_nelkul_2.png’ import kozepImg from ‘…/…/assets/images/kozep.png’ import emeltImg from ‘…/…/assets/images/emelt.png’
import ToggleBtn from ‘./ToggleBtn’ import Picker from ‘./Picker’
const TesztValaszto = ({navigation, route}) => {
const [isEnabled, setIsEnabled] = useState(false); const toggleSwitch = () => setIsEnabled(previousState => !previousState);
const { tantargy } = route.params;
useLayoutEffect(() => { navigation.setOptions({ title: tantargy }); }, [navigation, tantargy]);
return ( <View style={[styles.toggleBtnContainer, styles.left]}> Közép <View style={[styles.toggleBtnContainer, styles.right]}> Emelt
export default TesztValaszto
- Assistant
To achieve this behavior, you can create a parent component that will manage the state of the two toggle buttons, and then apply the new logic to the TesztValaszto component.
- Create a new parent component named
ToggleBtnGroupthat will handle the state and toggle logic:
import React, { useState } from "react"; import ToggleBtn from "./ToggleBtn"; const ToggleBtnGroup = ({ images }) => { const [isToggled, setIsToggled] = useState(0); const toggleHandler = (index) => { setIsToggled(index); }; return ( <> <ToggleBtn imgUrl={images[0]} isToggled={isToggled === 0} onPress={() => toggleHandler(0)} /> <ToggleBtn imgUrl={images[1]} isToggled={isToggled === 1} onPress={() => toggleHandler(1)} /> </> ); }; export default ToggleBtnGroup;- Update the
ToggleBtncomponent to accept theisToggledandonPressprops:
import React from "react"; import { TouchableOpacity, Image } from "react-native"; import styles from "./ToggleBtn.style"; const ToggleBtn = ({ imgUrl, isToggled, onPress }) => { return ( <TouchableOpacity style={isToggled ? styles.toggleBtnContainerOn : styles.toggleBtnContainer} onPress={onPress} value={isToggled} > <Image style={styles.toggleBtnImg} source={imgUrl} /> </TouchableOpacity> ); }; export default ToggleBtn;- Update the
TesztValasztocomponent to use the newToggleBtnGroupcomponent for both pairs of toggle buttons:
import React, { useState, useLayoutEffect } from "react"; import { View, Text, SafeAreaView, TouchableOpacity, Switch, } from "react-native"; import styles from "./TesztValaszto.style"; import { COLORS } from "../../constants"; import magyarazattalImg from "../../assets/images/magyarazattal_2.png"; import magyarazatNelkulImg from "../../assets/images/magyarazat_nelkul_2.png"; import kozepImg from "../../assets/images/kozep.png"; import emeltImg from "../../assets/images/emelt.png"; import ToggleBtn from "./ToggleBtn"; import Picker from "./Picker"; import ToggleBtnGroup from "./ToggleBtnGroup"; const TesztValaszto = ({ navigation, route }) => { // ... previous code ... return ( <SafeAreaView style={styles.screen}> <View style={styles.row1}> <ToggleBtnGroup images={[kozepImg, emeltImg]} /> </View> {/*.. row2 code ..*/} <View style={styles.row3}> <ToggleBtnGroup images={[magyarazattalImg, magyarazatNelkulImg]} /> </View> {/*.. row4 code ..*/} </SafeAreaView> ); }; export default TesztValaszto;Now, when you navigate to the
TesztValasztocomponent, one of the toggle buttons will be pressed by default, and pressing the other button will untoggle the first one. - Create a new parent component named
- User
I have the below togglebutton and I have 2 of them, I want them to work like this: Firstly, if I navigate to the page, one of the toggle button’s should already be pressed. Secondly, if I press the other toggle button, the first one should untoggle. Can you do this for me with a callback function?
import React, { useState } from “react” import { TouchableOpacity, Image} from ‘react-native’
import styles from ‘./ToggleBtn.style’
const ToggleBtn = (props) => { const [isToggled, setIsToggled] = useState(false); const toggleHandler = () => setIsToggled(previousState => !previousState);
return( <TouchableOpacity style={isToggled ? styles.toggleBtnContainerOn : styles.toggleBtnContainer} onPress={toggleHandler} value={isToggled}> ) }
export default ToggleBtn
import React, { useState, useLayoutEffect } from ‘react’ import { View, Text, SafeAreaView, TouchableOpacity, Switch } from ‘react-native’
import styles from ‘./TesztValaszto.style’ import { COLORS } from “…/…/constants”;
import magyarazattalImg from ‘…/…/assets/images/magyarazattal_2.png’ import magyarazatNelkulImg from ‘…/…/assets/images/magyarazat_nelkul_2.png’ import kozepImg from ‘…/…/assets/images/kozep.png’ import emeltImg from ‘…/…/assets/images/emelt.png’
import ToggleBtn from ‘./ToggleBtn’ import Picker from ‘./Picker’
const TesztValaszto = ({navigation, route}) => {
const [isEnabled, setIsEnabled] = useState(false); const toggleSwitch = () => setIsEnabled(previousState => !previousState);
const { tantargy } = route.params;
useLayoutEffect(() => { navigation.setOptions({ title: tantargy }); }, [navigation, tantargy]);
return ( <View style={[styles.toggleBtnContainer, styles.left]}> Közép <View style={[styles.toggleBtnContainer, styles.right]}> Emelt
export default TesztValaszto
- Assistant
Here's how you can make the desired change using a callback function:
- Update the
ToggleBtncomponent to accept atoggledprop and anonTogglecallback prop:
import React from "react"; import { TouchableOpacity, Image } from "react-native"; import styles from "./ToggleBtn.style"; const ToggleBtn = ({ imgUrl, toggled, onToggle }) => { return ( <TouchableOpacity style={toggled ? styles.toggleBtnContainerOn : styles.toggleBtnContainer} onPress={onToggle} value={toggled} > <Image style={styles.toggleBtnImg} source={imgUrl} /> </TouchableOpacity> ); }; export default ToggleBtn;- Create a
ToggleBtnGroupcomponent that will manage the state and handle the toggling logic using a callback:
import React, { useState } from "react"; import ToggleBtn from "./ToggleBtn"; const ToggleBtnGroup = ({ images }) => { const [toggledIndex, setToggledIndex] = useState(0); const handleToggle = (index) => { setToggledIndex(index); }; return ( <> <ToggleBtn imgUrl={images[0]} toggled={toggledIndex === 0} onToggle={() => handleToggle(0)} /> <ToggleBtn imgUrl={images[1]} toggled={toggledIndex === 1} onToggle={() => handleToggle(1)} /> </> ); }; export default ToggleBtnGroup;- Update the
TesztValasztocomponent to use theToggleBtnGroupcomponent for the toggle button pairs:
import React, { useState, useLayoutEffect } from "react"; import { View, Text, SafeAreaView, TouchableOpacity, Switch, } from "react-native"; import styles from "./TesztValaszto.style"; import { COLORS } from "../../constants"; import magyarazattalImg from "../../assets/images/magyarazattal_2.png"; import magyarazatNelkulImg from "../../assets/images/magyarazat_nelkul_2.png"; import kozepImg from "../../assets/images/kozep.png"; import emeltImg from "../../assets/images/emelt.png"; import ToggleBtn from "./ToggleBtn"; import Picker from "./Picker"; import ToggleBtnGroup from "./ToggleBtnGroup"; const TesztValaszto = ({ navigation, route }) => { // ... existing code ... return ( <SafeAreaView style={styles.screen}> <View style={styles.row1}> <ToggleBtnGroup images={[kozepImg, emeltImg]} /> </View> {/* ... row2 code ... */} <View style={styles.row3}> <ToggleBtnGroup images={[magyarazattalImg, magyarazatNelkulImg]} /> </View> {/* ... row4 code ... */} </SafeAreaView> ); }; export default TesztValaszto;Now, the
ToggleBtncomponents within theToggleBtnGroupcomponent will behave as desired. One will be toggled by default, and when the other is pressed, the first one will untoggle. - Update the
- User
I made a toggle button group to make the desired effect, my problem is that the 2 toggle button are under each other, I want one of them on the left side and one of them on the right side, and I also want a text above the toggle buttons
import React, { useState } from "react"; import ToggleBtn from "./ToggleBtn";
const ToggleBtnGroup = ({ images }) => { const [toggledIndex, setToggledIndex] = useState(0);
const handleToggle = (index) => { setToggledIndex(index); };
return ( <> <ToggleBtn imgUrl={images[0]} toggled={toggledIndex === 0} onToggle={() => handleToggle(0)} /> <ToggleBtn imgUrl={images[1]} toggled={toggledIndex === 1} onToggle={() => handleToggle(1)} /> </> ); };
export default ToggleBtnGroup;
import React from "react"; import { TouchableOpacity, Image } from "react-native";
import styles from "./ToggleBtn.style";
const ToggleBtn = ({ imgUrl, toggled, onToggle }) => { return ( <TouchableOpacity style={toggled ? styles.toggleBtnContainerOn : styles.toggleBtnContainer} onPress={onToggle} value={toggled} > ); };
export default ToggleBtn;
- Assistant
You can achieve this by adding a
Viewwrapper around eachToggleBtnand aTextabove the buttons, and applying custom styles using Flexbox.Update the
ToggleBtnGroupcomponent as follows:import React, { useState } from "react"; import { View, Text } from "react-native"; import ToggleBtn from "./ToggleBtn"; const ToggleBtnGroup = ({ images, texts }) => { const [toggledIndex, setToggledIndex] = useState(0); const handleToggle = (index) => { setToggledIndex(index); }; return ( <View style={{ flexDirection: "row", justifyContent: "space-between" }}> <View style={{ alignItems: "center" }}> <Text>{texts[0]}</Text> <ToggleBtn imgUrl={images[0]} toggled={toggledIndex === 0} onToggle={() => handleToggle(0)} /> </View> <View style={{ alignItems: "center" }}> <Text>{texts[1]}</Text> <ToggleBtn imgUrl={images[1]} toggled={toggledIndex === 1} onToggle={() => handleToggle(1)} /> </View> </View> ); }; export default ToggleBtnGroup;Now you need to pass an additional
textsprop when using theToggleBtnGroupcomponent, with text values for both buttons:import React from "react"; import { View } from "react-native"; import ToggleBtnGroup from "./ToggleBtnGroup"; // ...other imports... const TesztValaszto = ({ navigation, route }) => { // ...other code... return ( <SafeAreaView style={styles.screen}> <View style={styles.row1}> <ToggleBtnGroup images={[kozepImg, emeltImg]} texts={["Közép", "Emelt"]} /> </View> {/* ...rest of the code... */} </SafeAreaView> ); }; export default TesztValaszto;Now you will have the two toggle buttons side by side, with a text label above each button.
- User
I now have 2 toggle buttons, with the text above them, but my styles won't apply to the toggle button group. How can I solve this?
import React, { useState } from "react"; import { View, Text } from "react-native"; import ToggleBtn from "./ToggleBtn";
const ToggleBtnGroup = ({ images, texts }) => { const [toggledIndex, setToggledIndex] = useState(0);
const handleToggle = (index) => { setToggledIndex(index); };
return ( <View style={{ flexDirection: "row", justifyContent: "space-between" }}> <View style={{ alignItems: "center" }}> {texts[0]} <ToggleBtn imgUrl={images[0]} toggled={toggledIndex === 0} onToggle={() => handleToggle(0)} />
<View style={{ alignItems: "center" }}> <Text>{texts[1]}</Text> <ToggleBtn imgUrl={images[1]} toggled={toggledIndex === 1} onToggle={() => handleToggle(1)} /> </View> </View>); };
export default ToggleBtnGroup;
import React from "react"; import { TouchableOpacity, Image } from "react-native";
import styles from "./ToggleBtn.style";
const ToggleBtn = ({ imgUrl, toggled, onToggle }) => { return ( <TouchableOpacity style={toggled ? styles.toggleBtnContainerOn : styles.toggleBtnContainer} onPress={onToggle} value={toggled} > ); };
export default ToggleBtn;
import { StyleSheet } from "react-native";
import { COLORS, FONT, SIZES } from "../../constants";
const styles = StyleSheet.create({ toggleBtnContainer: { flex: 1, backgroundColor: COLORS.offWhite, padding: 20, borderRadius: 20, shadowOffset: { width: 12, height: 12 }, shadowRadius: 12, shadowColor: '#0a234d', shadowOpacity: 0.5, elevation: 10, }, toggleBtnContainerOn: { flex: 1, padding: 15, borderRadius: 20, backgroundColor: COLORS.accentYellow, borderColor: COLORS.primary, borderWidth: 5, }, toggleBtnImg: { width: '100%', resizeMode: 'contain', flex: 1, }, })
export default styles
- Assistant
You can pass custom styles as a prop to the
ToggleBtnGroupcomponent and apply them to the wrapperView. Here's how you can do it:- Add a
containerStyleprop to theToggleBtnGroupcomponent and apply it to the wrapperView:
import React, { useState } from "react"; import { View, Text } from "react-native"; import ToggleBtn from "./ToggleBtn"; const ToggleBtnGroup = ({ images, texts, containerStyle }) => { // ...previous code... return ( <View style={[{ flexDirection: "row", justifyContent: "space-between" }, containerStyle]}> {/* ...previous code... */} </View> ); }; export default ToggleBtnGroup;- Pass the custom styles to the
ToggleBtnGroupcomponent in theTesztValasztocomponent:
import React from "react"; import { View } from "react-native"; import ToggleBtnGroup from "./ToggleBtnGroup"; // ...other imports... const TesztValaszto = ({ navigation, route }) => { // ...other code... return ( <SafeAreaView style={styles.screen}> <View style={styles.row1}> <ToggleBtnGroup images={[kozepImg, emeltImg]} texts={["Közép", "Emelt"]} containerStyle={styles.toggleBtnGroupContainer} /> </View> {/* ...rest of the code... */} </SafeAreaView> ); }; export default TesztValaszto;- Finally, add the
toggleBtnGroupContainerstyle to yourstylesobject:
const styles = StyleSheet.create({ // ...previous styles... toggleBtnGroupContainer: { // Customize styles for the ToggleBtnGroup container }, }); export default styles;Now, the custom styles you define in the
toggleBtnGroupContainerstyle will be applied to theToggleBtnGroupcomponent. - Add a
- User
What is the best local database I can use in a react native app? I have only strings/numbers/booleans in my data and I want it to be as fast as possible
- Assistant
The best local databases for React Native applications, considering your requirement for handling strings, numbers, and booleans, and focusing on performance, are:
-
Realm: Realm is a powerful, easy-to-use local database that provides a fast and efficient solution to store and retrieve data in React Native apps. It supports strings, numbers, booleans, and complex data types like lists and objects. Realm offers seamless integration with React Native, providing a great developer experience with live objects, change notifications, and queries. It is also compatible with both iOS and Android.
-
SQLite: SQLite is another popular choice for local storage in React Native applications. It is a self-contained, serverless, zero-configuration SQL database engine that supports strings, numbers, booleans, and more. There are several libraries available to use SQLite with React Native, such as
react-native-sqlite-storageandreact-native-sqlite-2. SQLite is known for its efficiency, reliability, and ease of use, and works well for simple to moderately complex applications. -
WatermelonDB: WatermelonDB is a high-performance, observable, and lazy-loaded database for React Native apps. It is built on top of SQLite and is specifically designed for scaling large datasets in mobile applications. WatermelonDB supports strings, numbers, booleans, and relationships between models. Its key features include offline-first support, a reactive API, and advanced indexing and querying capabilities.
-
PouchDB: PouchDB is an open-source JavaScript database inspired by Apache CouchDB that is designed for local storage in web and mobile apps. It supports strings, numbers, booleans, and other data types. PouchDB automatically syncs with a remote database, making it suitable for offline-first applications. There are libraries available to use PouchDB in React Native, such as
pouchdb-react-native.
All of these databases have their pros and cons. Which one you choose depends on your specific requirements, personal preferences, and the complexity of your application. Realm and SQLite are popular choices for general-purpose React Native applications, while WatermelonDB and PouchDB are more suitable for specific use-cases with large-scale, offline-first, or sync-heavy apps.
-
- User
For a local database with 13000 record where I only need to find 30 record at a time, is watermelondb or sqlite better for a react native application?
- Assistant
In your case, with 13,000 records and a requirement to query 30 records at a time, both WatermelonDB and SQLite are viable options. However, each has its own advantages, and your choice should depend on your specific needs and preferences.
WatermelonDB:
- Built on top of SQLite, specifically designed for React Native.
- Optimized for large-scale and complex applications.
- Offers a reactive API and seamless integration with React.
- Lazy-loading for efficient performance.
- Offline-first support with automatic synchronization.
SQLite:
- Widely used, well-established local database engine.
- Suitable for simple to moderately complex applications.
- Numerous libraries available for React Native integration, such as
react-native-sqlite-storageandreact-native-sqlite-2. - May require more manual work to set up reactive UI updates.
- Smaller learning curve compared to WatermelonDB if you're already familiar with SQL.
In summary, if you need advanced features like lazy-loading, reactive API, offline support, and you're working with a large-scale or potentially complex app, then WatermelonDB would be the better choice. On the other hand, if you're looking for a straightforward, well-established solution and you are comfortable with SQL, SQLite would be a suitable option.
Both options are capable of handling your requirements, but your choice should be based on your specific needs, your familiarity and comfort with the technology, and your app's complexity.