<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>Hanzala Gün</title>
	<atom:link href="https://hanzala.com.tr/en/feed/" rel="self" type="application/rss+xml" />
	<link>https://hanzala.com.tr</link>
	<description>Personal Website</description>
	<lastBuildDate>Sun, 25 Jul 2021 14:09:22 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.3</generator>
<site xmlns="com-wordpress:feed-additions:1">195071820</site>	<item>
		<title>Simple word memorization app with React Native + Realm + Zustand</title>
		<link>https://hanzala.com.tr/2021/07/25/react-native-realm-zustand-2/</link>
					<comments>https://hanzala.com.tr/2021/07/25/react-native-realm-zustand-2/#respond</comments>
		
		<dc:creator><![CDATA[Muhammed Hanzala Gün]]></dc:creator>
		<pubDate>Sun, 25 Jul 2021 14:09:17 +0000</pubDate>
				<category><![CDATA[ReactNative]]></category>
		<category><![CDATA[React Native]]></category>
		<category><![CDATA[React native blur]]></category>
		<category><![CDATA[react native icon]]></category>
		<category><![CDATA[react-native-vector-icons]]></category>
		<category><![CDATA[Realm]]></category>
		<category><![CDATA[zustand]]></category>
		<guid isPermaLink="false">https://hanzala.com.tr/?p=436</guid>

					<description><![CDATA[Introduction Hello. I am sharing this post to share a simple application that I developed for my lack of English...]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">Introduction</h2>



<p>Hello. I am sharing this post to share a simple application that I developed for my lack of English words and to give a short example about the use of Realm, Zustand. I made this application on &#8220;Preply&#8221; of my teacher Fabian Afumbom A., which he used while learning a language, during the periods when I was taking lessons in the Preply application. Well, without further ado, let&#8217;s get to the point.</p>



<h2 class="wp-block-heading">Packages Used</h2>



<ul><li><a href="https://github.com/realm/realm-js" target="_blank" rel="noreferrer noopener">realm</a></li><li><a href="https://github.com/pmndrs/zustand" target="_blank" rel="noreferrer noopener">zustand</a></li><li><a href="https://github.com/oblador/react-native-vector-icons" target="_blank" rel="noreferrer noopener">react-native-vector-icons</a></li><li><a href="https://github.com/Kureev/react-native-blur" target="_blank" rel="noreferrer noopener">@react-native-community/blur</a></li></ul>



<h2 class="wp-block-heading">Package Installations (React native v0.64.2)</h2>



<p>First, we open our project in terminal and create it with react native.</p>



<pre class="wp-block-code"><code>npx react-native init FabianEnglish</code></pre>



<p>you can replace “FabianEnglish” with a project name of your choice.</p>



<p>Then we proceed to the project directory</p>



<pre class="wp-block-code"><code>cd FabianEnglish</code></pre>



<p>and we install realm</p>



<pre class="wp-block-code"><code>npm install realm</code></pre>



<p>then we install the pods</p>



<pre class="wp-block-code"><code>cd ios &amp;&amp; pod install &amp;&amp; cd ..</code></pre>



<p>that&#8217;s it for realm.</p>



<p>It&#8217;s time to install zustand. If you are sure that you are in the root directory, paste the code below into the terminal again and complete the zustand installation.</p>



<pre class="wp-block-code"><code>npm install zustand </code></pre>



<p>or with yarn</p>



<pre class="wp-block-code"><code>yarn add zustand</code></pre>



<p>it&#8217;s time to install react native vector icons. Icons are always needed so that the project looks a little better :). Let&#8217;s add our package.</p>



<pre class="wp-block-code"><code>npm install --save react-native-vector-icons</code></pre>



<p>Finally, let&#8217;s add our blur package</p>



<pre class="wp-block-code"><code>yarn add @react-native-community/blur</code></pre>



<p>or</p>



<pre class="wp-block-code"><code>npm install --save @react-native-community/blur</code></pre>



<p>and finally, let&#8217;s install the pods for ios</p>



<pre class="wp-block-code"><code>cd ios &amp;&amp; pod install &amp;&amp; cd ..</code></pre>



<p>paket kurulumu tamamlandı.</p>



<h2 class="wp-block-heading">This Project&#8217;s GitHub page</h2>



<p><a href="https://github.com/HanzalaGun/FabianEnglish" target="_blank" rel="noopener">https://github.com/HanzalaGun/FabianEnglish</a></p>



<h2 class="wp-block-heading">Let&#8217;s Code the Project</h2>



<p>First, let&#8217;s create our folders to split the project into parts. We create a folder named src in the root directory and 4 folders named common, components, screen, state under this folder. Then we create a file named ListScreen.js in the screen.</p>



<figure class="wp-block-image size-large"><img decoding="async" width="221" height="128" src="https://hanzala.com.tr/wp-content/uploads/2021/06/Ekran-Resmi-2021-06-28-16.10.27.png" alt="" class="wp-image-362" title="Simple word memorization app with React Native + Realm + Zustand 1"></figure>



<p>we can write this file&#8217;s code directly in App.js. Awe can show this file directly in App.js. Actually, for more complex structures, we could code what we would code directly into App.js , but if we want to grow the project in the future and the pages increase, let&#8217;s keep our structure ready so that there is no confusion :). App.js will look like this.</p>



<pre class="wp-block-code"><code>import React from 'react'
import ListScreen from './src/screens/ListScreen'

const App = () =&gt; {
  return (
      &lt;ListScreen/&gt;
  )
}
export default App
</code></pre>



<p>artık ListScreen.js eNow we can move on to ListScreen.js. First of all, I wanted to give a background photo and a blur in front of this photo. For this, we put a background photo we want in the common folder with the name bg.jpg. this name is not important, just make sure it has the same name when you import the picture. for import this picture we can use this code</p>



<pre class="wp-block-code"><code>const bg= require('../common/bg.jpg')</code></pre>



<p>We transfer it to ListScreen.js with. Let&#8217;s import our icons</p>



<pre class="wp-block-code"><code>import Icon from 'react-native-vector-icons/FontAwesome';</code></pre>



<p>and to use this icon</p>



<pre class="wp-block-code"><code>Icon.loadFont()</code></pre>



<p>We need to load the icons by typing the above code snippet into the page. In the meantime, let&#8217;s prepare a small component for the blur background. We create a file called MyBlur.js in components and use our “@react-native-community/blur” plugin here. Our MyBlur.js file looks like this</p>



<pre class="wp-block-code"><code>import React from 'react'
import { StyleSheet } from 'react-native'
import { BlurView } from "@react-native-community/blur";

const MyBlur = () =&gt; {
    return (
        &lt;BlurView
            style={styles.absolute}
            blurType="dark"
            /&gt;
    )
}

export default MyBlur

const styles = StyleSheet.create({
    absolute: {
        position: "absolute",
        top: 0,
        left: 0,
        bottom: 0,
        right: 0,
      }
})</code></pre>



<p>Let&#8217;s go back to our ListScreen.js page. On this page, FabianEnglish will be written on the left in the header section. On the right, there will be two buttons, one of which will be “Ask Me” and the other will be “Learn”. There will be a modal where we will translate the word that we recorded for us with Ask Me, and if it is correct, the score of the word will increase. There will be a skip button under it and if we press this button, the score will decrease. There will be a search input under the header so that we can search for words. Then there will be a button that we position as absolute at the bottom right, when we press this button, the section where we can save the word will become visible. Finally, our words will be listed in the body section. Before we forget, let&#8217;s create a TextInput of our own. So first go to the components folder</p>



<ul><li>Askme.js</li><li>Learn.js </li><li>SendComp.js(the component we will save the word)</li><li>ListComp.js(component to list words )</li><li>MyTextInput.js</li></ul>



<p>We create this pages.</p>



<p>Finally, let&#8217;s create our state management without going back to ListScreen.</p>



<p>I created a file named state.js in our state folder. Yes, let&#8217;s start using Zustand, which is one of the main topics. Here we can say that we create our state and actions. But it was much easier to use than redux :). Let&#8217;s first share the codes in state.js and then try to explain.</p>



<pre class="wp-block-code"><code>import create from 'zustand'

export const useStore = create(set =&gt; ({
  wordState:false,
  popupState:false,
  askmeState:false,
  learnState:false,
  selectedWordState:{},
  search:"",

  setWordState:(data)=&gt;set((state)=&gt;({...state,wordState:data})),
  setPopupState:(data)=&gt;set((state)=&gt;({...state,popupState:data})),
  setAskmeState:(data)=&gt;set((state)=&gt;({...state,askmeState:data})),
  setLearnState:(data)=&gt;set((state)=&gt;({...state,learnState:data})),
  setSelectedWordState:(data)=&gt;set((state)=&gt;({...state,selectedWordState:data})),
  setSearch:(data)=&gt;set((state)=&gt;({...state,search:data})),
  
}))
</code></pre>



<p>Firstly, </p>



<p></p>



<p>First, we call the create function from zustand, assign this function to a variable named useStore, export it and create an array function with the set parameter in our create function. and we define our variables such as wordState, learnState, search as variables. We value it as necessary, such as object, string, int, boolean.. We can say that our variables that start with the set prefix are our actions, we will use them to update the states we have defined above. For more, you can dive into zustand&#8217;s documentation :).</p>



<p>but to read state basicly</p>



<pre class="wp-block-code"><code>    const loading = useStore(state =&gt; state.wordState)</code></pre>



<p>for update the state</p>



<pre class="wp-block-code"><code>    const setLoading = useStore(state =&gt; state.setWordState)</code></pre>



<p>we can use this.</p>



<p>Yes, now we are finishing ListScreen.js and we are writing it like this.</p>



<pre class="wp-block-code"><code>import React from 'react'
import { StyleSheet, Text, SafeAreaView, View, Pressable, ImageBackground } from 'react-native'
import Icon from 'react-native-vector-icons/FontAwesome';
import AskMe from '../components/AskMe';
import Learn from '../components/Learn';
import ListComp from '../components/ListComp'
import SendComp from '../components/SendComp'
import MyTextInput from '../components/MyTextInput'
import { useStore } from '../state/atom';
import MyBlur from '../components/MyBlur';

const bg= require('../common/bg.jpg')
Icon.loadFont()

  
const ListScreen = () =&gt; {
    const setAskVisible = useStore(state =&gt; state.setAskmeState)
    const setLearnVisible = useStore(state =&gt; state.setLearnState)
    const setSearch = useStore(state =&gt; state.setSearch)
    const search=(data)=&gt;{
       setSearch(data)
    }
    return (
        &lt;ImageBackground 
        source={bg}
        resizeMode="stretch"
        style={{backgroundColor:'white'}}
        &gt;
             &lt;MyBlur/&gt;
             &lt;View style={styles.head1}&gt;
                    &lt;SafeAreaView&gt;
                        &lt;View style={styles.head}&gt;
                            &lt;Text style={styles.title}&gt;Fabian English |&lt;/Text&gt;
                            &lt;Pressable style={styles.pressable} 
                                onPress={()=&gt;setAskVisible(true)}&gt;
                                &lt;Icon size={30} name="question" color="white" style={styles.pressableicon}&gt;&lt;/Icon&gt;
                                &lt;Text style={styles.headtext}&gt;ask me&lt;/Text&gt;
                            &lt;/Pressable&gt;
                            &lt;Pressable style={styles.pressable}
                                onPress={()=&gt;setLearnVisible()}&gt;
                                &lt;Icon size={30} name="book" color="white" style={styles.pressableicon}&gt;&lt;/Icon&gt;
                                &lt;Text style={styles.headtext}&gt;learn&lt;/Text&gt;
                            &lt;/Pressable&gt;
                        &lt;/View&gt;
                    &lt;/SafeAreaView&gt;
            &lt;/View&gt;
            &lt;AskMe/&gt;
            &lt;Learn/&gt;
            &lt;SafeAreaView style={styles.container}&gt;
                &lt;View&gt;
                    &lt;MyTextInput placeholder="Search" onChange={(data)=&gt;search(data)} padding={20}/&gt;
                &lt;/View&gt;
                &lt;ListComp/&gt;

               
            &lt;/SafeAreaView&gt;
                    &lt;SendComp/&gt;
           
        &lt;/ImageBackground&gt;


    )
}

export default ListScreen

const styles = StyleSheet.create({
    container:{
        height:'100%',
        
    },
    head:{
        flexDirection:'row',
        justifyContent:'space-around',
        marginBottom:10,
        alignItems:'center',
        paddingVertical:10,
    },
    head1:{
        marginBottom:10,
        backgroundColor:'#ffffff50',
        shadowColor:'black',
        shadowOpacity:0.,
        shadowRadius:1,
        shadowOffset:{width: -1, height: 3},
    },
    
    title:{
        color:'white',
        fontSize:20,
        fontWeight:'700',
        textShadowColor:'black',
        textShadowRadius:3,
        textShadowOffset:{width: -1, height: 1},
    },
   
    headtext:{
        color:'white',
        fontSize:14,
        fontWeight:'600',
        textShadowColor:'black',
        textShadowRadius:6,
        textShadowOffset:{width: -1, height: 1},
        paddingLeft:3
    },
    pressable:{
        flexDirection:'row',
        alignItems:'center'
    },
    pressableicon:{
        shadowColor:'black',
        shadowOpacity:0.2,
        shadowRadius:1,
        shadowOffset:{width: -1, height: 3},
    },
})
</code></pre>



<p>useStore ile zustand a nasıl veri gönderebileceğimizi görmüşsündür, bu kadar kYou have seen how we can send data to a zustand with useStore, it&#8217;s that easy 🙂</p>



<p>Let&#8217;s share the other components in turn.</p>



<pre class="wp-block-code"><code>//MyTextInput.js
import React from 'react'
import { StyleSheet, View, TextInput, Dimensions } from 'react-native'

const MyTextInput = ({onChange,placeholder,value,autoFocus=false,padding=70}) =&gt; {

    return (
        &lt;View style={&#91;styles.container,{width:Dimensions.get('screen').width-padding}]}&gt;
            &lt;TextInput style={{paddingVertical:5}} autoFocus={autoFocus} value={value} placeholder={placeholder} placeholderTextColor='white' onChangeText={(a)=&gt;{onChange(a)}}&gt;&lt;/TextInput&gt;
            
        &lt;/View&gt;
    )
}

export default MyTextInput

const styles = StyleSheet.create({
    container:{
        backgroundColor:'#f2f2f270',
        padding:5,
        margin:3,
        alignSelf:'center',
        borderRadius:5,
    }
})
</code></pre>



<p>Let&#8217;s take a look at the important issue, Realm, which intervenes here.</p>



<pre class="wp-block-code"><code>import Realm from 'realm'</code></pre>



<p>To use it, we need to import it as above. And we need to use a schema. Since we will use this scheme in a different way, let&#8217;s put it in a separate file for a cleaner look and ease of use. Let&#8217;s create a Shema.js file in our public folder.</p>



<pre class="wp-block-code"><code>export const WordShema = {
    name: 'Words',
    properties: {
        id:'int',
        name:  'string',
        description: 'string',
        pronunciation: 'string',
        translate: 'string',
        date: 'date',
        score: 'int'
    }
  };</code></pre>



<p>We don&#8217;t have much to tell in this. First &#8220;name&#8221; is our shema&#8217;s name . Other keys are our datas veriable in the properties.</p>



<ul><li>id =&gt; identical id</li><li>name =&gt; our word what we want to learn</li><li>description =&gt; if we want to add a description at our word</li><li>pronunciation =&gt; pronuncitation</li><li>translate =&gt; translated word </li><li>date =&gt; data&#8217;s created date</li><li>score =&gt; the word&#8217;s score</li></ul>



<p>let&#8217;s go to usage</p>



<pre class="wp-block-code"><code>import { WordShema } from '../common/Shema';</code></pre>



<p>we import our shema</p>



<p>öncelikleFirst of all, we tell which scheme we will open using the realmi function opening function, and after opening it, we write what we do.</p>



<pre class="wp-block-code"><code>Realm.open({schema:&#91;WordShema]}).then(realm=&gt;{
   //we are always use this area .
})</code></pre>



<p>For example we want to get all datas</p>



<pre class="wp-block-code"><code>Realm.open({schema:&#91;WordShema]}).then(realm=&gt;{
   let obj = realm.objects('Words');
})</code></pre>



<p>İf we want to write a data to our database</p>



<pre class="wp-block-code"><code> Realm.open({schema:&#91;WordShema]}).then(realm=&gt;{
                realm.write(()=&gt;{
                    const myData=realm.create('Words',{
                        id:'benzersiz id',
                        name:'kelime',
                        description:'açıklama',
                        pronunciation:'telafuz',
                        translate:'çeviri',
                        date:new Date(),
                        score:0
                    })
                })
            })</code></pre>



<p>we can use this.</p>



<p>or update a data in realm,</p>



<pre class="wp-block-code"><code>Realm.open({schema:&#91;WordShema]}).then(realm=&gt;{
                let obj = realm.objects('Words').filtered("id='"+data.id+"'")&#91;0];
                realm.write(() =&gt; {
                obj.name='güncellenen kelime'
                })
            })</code></pre>



<p>we can use this. </p>



<p>Now that we see the use of realm as a basis, we can return to our codes. Let&#8217;s continue with the components and even though I share them all one after the other, there is no part you can&#8217;t understand :). I don&#8217;t need any further explanation 🙂</p>



<pre class="wp-block-code"><code>//SendComp.js
import React, {useState, useEffect} from 'react'
import { StyleSheet, Text, View, TouchableOpacity, Dimensions, Alert, SafeAreaView } from 'react-native'
import MyTextInput from './MyTextInput'
import Realm from 'realm';
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
import {  useStore } from '../state/state';
import { WordShema } from '../common/Shema';
const width=Dimensions.get('screen').width;

Icon.loadFont()
const SendComp = ({isSend=true}) =&gt; {
    const data = useStore(state =&gt; state.selectedWordState)
    const &#91;name, setName] = useState('')
    const &#91;description, setDescription] = useState('')
    const &#91;pronunciation, setPronunciation] = useState('')
    const &#91;translate, setTranslate] = useState('')
    const &#91;opened, setOpened] = useState(isSend?false:true)
    const setLoading = useStore(state =&gt; state.setWordState)
    const setModalVisible = useStore(state =&gt; state.setPopupState)

    useEffect(() =&gt; {
        if(!isSend&amp;&amp;data){
            setName(data.name)
            setDescription(data.description)
            setPronunciation(data.pronunciation)
            setTranslate(data.translate)

        }
    }, &#91;data])
    
    const sendData=()=&gt;{
        if(name.length&gt;0&amp;&amp;translate.length&gt;0){
            Realm.open({schema:&#91;WordShema]}).then(realm=&gt;{
                realm.write(()=&gt;{
                    const myData=realm.create('Words',{
                        id:parseInt(new Date().getTime()+''+Math.floor(Math.random()*10000)),
                        name:name,
                        description:description,
                        pronunciation:pronunciation,
                        translate:translate,
                        date:new Date(),
                        score:0
                    })
                })
            }).then(()=&gt;{
                setLoading(true)
                setName('')
                setDescription('')
                setPronunciation('')
                setTranslate('')
                setModalVisible(false)
            })
        }else{
            Alert.alert('You must be fill Word and Translate')
        }
        
    }
    const updateData = ()=&gt;{
        if(name.length&gt;0&amp;&amp;translate.length&gt;0){

            Realm.open({schema:&#91;WordShema]}).then(realm=&gt;{
                let obj = realm.objects('Words').filtered("id='"+data.id+"'")&#91;0];
                realm.write(() =&gt; {
                obj.name=name
                obj.description=description
                obj.pronunciation=pronunciation
                obj.translate=translate
                })
            }).then(()=&gt;{
                setLoading(true)
                setModalVisible(false)
            })
        }else{
            Alert.alert('You must be fill Word and Translate')
        }
    }
    return (
        &lt;SafeAreaView style={&#91;styles.container,isSend?{position:'absolute',
        bottom:150,
        left:10}:null]}&gt;
            {opened&amp;&amp;&lt;View style={styles.inputs}&gt;
               
                &lt;MyTextInput value={name} placeholder='Word' onChange={(data)=&gt;{setName(data)}}&gt;&lt;/MyTextInput&gt;
                &lt;MyTextInput value={translate} placeholder='Translate' onChange={(data)=&gt;{setTranslate(data)}}&gt;&lt;/MyTextInput&gt;
                &lt;MyTextInput value={pronunciation} placeholder='Pronunciation' onChange={(data)=&gt;{setPronunciation(data)}}&gt;&lt;/MyTextInput&gt;
                &lt;MyTextInput value={description} placeholder='Description' onChange={(data)=&gt;{setDescription(data)}}&gt;&lt;/MyTextInput&gt;
            &lt;/View&gt;}
            &lt;View&gt;
                {opened?
                &lt;View style={{flex:1}}&gt;
                &lt;TouchableOpacity style={styles.buttonsend} onPress={()=&gt;isSend?sendData():updateData()}&gt;
                    &lt;Icon name={isSend?"send":'update'} size={isSend?30:50} color="green" /&gt;
                &lt;/TouchableOpacity&gt;
                {isSend&amp;&amp;&lt;TouchableOpacity style={styles.buttonminus} onPress={()=&gt;setOpened(false)}&gt;
                    &lt;Icon name="minus" size={30} color="red" /&gt;
                &lt;/TouchableOpacity&gt;}
                &lt;/View&gt;
                :
                &lt;View style={{alignItems:'flex-end', width:width-35,}}&gt;

                &lt;TouchableOpacity style={styles.buttonplus} onPress={()=&gt;setOpened(true)}&gt;
                    &lt;Icon style={styles.plusicon} name="plus" size={30} color="white" /&gt;
                &lt;/TouchableOpacity&gt;
                &lt;/View&gt;
                }
            &lt;/View&gt;

        &lt;/SafeAreaView&gt;
    )
}

export default SendComp

const styles = StyleSheet.create({
    container:{
        flexDirection:'row',
        paddingHorizontal:10,
        
    },
    buttonplus:{
        backgroundColor:'#fff5',
        padding:10,
        borderRadius:50,
        borderWidth:1,
        borderColor:'white',
        shadowColor:'black',
        shadowOpacity:0.5,
        shadowRadius:1,
        shadowOffset:{width: -1, height: 3},
    },
    buttonminus:{
        position:'absolute',
        bottom:-10
    },
    buttonsend:{
        justifyContent:'center',
        flex:1,
    },
    plusicon:{
        shadowColor:'black',
        shadowOpacity:0.5,
        shadowRadius:1,
        shadowOffset:{width: -1, height: 3},
    },
    inputs:{
        backgroundColor:'#111',
        borderRadius:5
    }
})
</code></pre>



<pre class="wp-block-code"><code>//Askme.js
import React, { useEffect, useState } from 'react'
import { StyleSheet, Dimensions, View,Modal, TouchableOpacity, Text } from 'react-native'
import Realm from 'realm'
import { useStore } from '../state/atom';
import { WordShema } from '../common/Shema';
import MyTextInput from './MyTextInput';
import MyBlur from './MyBlur';
const AskMe = () =&gt; {
    const visible = useStore(state =&gt; state.askmeState)
    const setVisible = useStore(state =&gt; state.setAskmeState)
    const &#91;data, setData] = useState(&#91;])
    const &#91;word, setWord] = useState('')
    const &#91;index, setIndex] = useState(0)
    useEffect(() =&gt; {
        Realm.open({schema:&#91;WordShema]}).then(realm=&gt;{
            setData(realm.objects('Words').sorted('score',true))
        })
    }, &#91;])
    useEffect(() =&gt; {
        if(data.length&gt;0&amp;&amp;word.toLowerCase()==data&#91;index].translate.toLowerCase()){
            //realmda kelimenin scorunu yükselt
            Realm.open({schema:&#91;WordShema]}).then(realm=&gt;{
                let obj = realm.objects('Words').filtered("id='"+data&#91;index].id+"'")&#91;0];
                realm.write(() =&gt; {
                    obj.score++
                })
            }).then(()=&gt;{
                setIndex(data.length!=index+1?index+1:0)
                setWord('')
            })
        }
    }, &#91;word])
    const skip =()=&gt;{
        //realmda kelimenin scorunu düşür
        if(data.length&gt;0){
            Realm.open({schema:&#91;WordShema]}).then(realm=&gt;{
                let obj = realm.objects('Words').filtered("id='"+data&#91;index].id+"'")&#91;0];
                realm.write(() =&gt; {
                    obj.score--
                })
                console.log(obj)
            }).then(()=&gt;{
                setIndex(data.length!=index+1?index+1:0)
                setWord('')
            })
        }
    }
    return (
        &lt;Modal
            animationType='slide'
            transparent={true}
            visible={visible}
        &gt;
            &lt;MyBlur/&gt;
            &lt;TouchableOpacity style={styles.view} onPress={()=&gt;setVisible(false)}&gt;
                &lt;Text style={styles.title}&gt;{data.length&gt;0&amp;&amp;data&#91;index].name}&lt;/Text&gt;
                &lt;Text style={styles.text}&gt;{data.length&gt;0&amp;&amp;'( '+data&#91;index].pronunciation+' )'}&lt;/Text&gt;
                &lt;Text style={styles.text}&gt;{data.length&gt;0&amp;&amp;data&#91;index].description}&lt;/Text&gt;
                &lt;MyTextInput
                    value={word}
                    autoFocus placeholder="Write Translate"
                    onChange={(data)=&gt;{setWord(data)}}
                &gt;&lt;/MyTextInput&gt;
                &lt;TouchableOpacity style={styles.skip} onPress={()=&gt;skip()}&gt;
                    &lt;Text style={styles.skiptext}&gt;{'skip &gt;&gt;'}&lt;/Text&gt;
                &lt;/TouchableOpacity&gt;
                &lt;View style={{height:40}}/&gt;
                &lt;Text style={styles.bottom}&gt;if you skip this word, the word's score to less&lt;/Text&gt;
                &lt;Text style={styles.bottom}&gt;if you learned this word, you must delete this word&lt;/Text&gt;
                
            &lt;/TouchableOpacity&gt;
        &lt;/Modal&gt;
    )
}

export default AskMe

const styles = StyleSheet.create({
    view:{
        width:'100%',
        height:'100%',
        alignItems:'center',
        justifyContent:'center',
        alignSelf:'center',
        borderRadius:10
    },
    skiptext:{
        color:'white',
        fontWeight:'900'
    },
      skip:{
          padding:10,
          marginTop:20,
          backgroundColor:'#f005',
          alignItems:'flex-end',
          width:Dimensions.get('screen').width-70,
          borderRadius:5
      },
      title:{
        color:'white',
        fontSize:25,
        fontWeight:'900',
        textShadowColor:'#f00',
        textShadowRadius:5,
        textShadowOffset:{width: 0, height: 0},
        marginBottom:20
    },
    bottom:{
        color:'white',
        fontSize:12
    },
    text:{
        color:'white'
    }
})
</code></pre>



<pre class="wp-block-code"><code>//Learn.js
import React, { useEffect, useState } from 'react'
import { StyleSheet, Dimensions, View,Modal, TouchableOpacity, Text } from 'react-native'
import Realm from 'realm'
import { useStore } from '../state/atom';
import { WordShema } from '../common/Shema';
import MyBlur from './MyBlur';
const Learn = () =&gt; {
    const visible = useStore(state =&gt; state.learnState)
    const setVisible = useStore(state =&gt; state.setLearnState)
    const &#91;data, setData] = useState(&#91;])
    const &#91;index, setIndex] = useState(0)
    useEffect(() =&gt; {
        Realm.open({schema:&#91;WordShema]}).then(realm=&gt;{
            setData(realm.objects('Words').sorted('score',true))
        })
    }, &#91;])
    
    const skip =()=&gt;{
            setIndex(data.length!=index+1?index+1:0)
    }
    return (
        &lt;Modal
            animationType='slide'
            transparent={true}
            visible={visible}
        &gt;
            &lt;MyBlur/&gt;
            &lt;TouchableOpacity style={styles.view} onPress={()=&gt;setVisible(false)}&gt;
                &lt;Text style={styles.title}&gt;{data.length&gt;0&amp;&amp;data&#91;index].name}&lt;/Text&gt;
                &lt;Text style={styles.text}&gt;{data.length&gt;0&amp;&amp;'( '+data&#91;index].pronunciation+' )'}&lt;/Text&gt;
                &lt;Text style={styles.text}&gt;{data.length&gt;0&amp;&amp;data&#91;index].translate}&lt;/Text&gt;
                &lt;Text style={styles.text}&gt;{data.length&gt;0&amp;&amp;data&#91;index].description}&lt;/Text&gt;
               
                &lt;TouchableOpacity style={styles.skip} onPress={()=&gt;skip()}&gt;
                    &lt;Text style={styles.skiptext}&gt;{'next &gt;&gt;'}&lt;/Text&gt;
                &lt;/TouchableOpacity&gt;
                &lt;View style={{height:40}}/&gt;
                
            &lt;/TouchableOpacity&gt;
        &lt;/Modal&gt;
    )
}

export default Learn

const styles = StyleSheet.create({
    view:{
        width:'100%',
        height:'100%',
        alignItems:'center',
        justifyContent:'center',
        alignSelf:'center',
        borderRadius:10
    },
    skiptext:{
        color:'white',
        fontWeight:'900'
    },
      skip:{
          padding:10,
          marginTop:20,
          backgroundColor:'#f005',
          alignItems:'center',
          width:Dimensions.get('screen').width-70,
          borderRadius:5
      },
      title:{
        color:'white',
        fontSize:25,
        fontWeight:'900',
        textShadowColor:'#f00',
        textShadowRadius:5,
        textShadowOffset:{width: 0, height: 0},
        marginBottom:20
    },
    text:{
        color:'white'
    }
})</code></pre>



<pre class="wp-block-code"><code>//ListComp.js
import React, {useState, useEffect} from 'react'
import { View, Dimensions, ScrollView } from 'react-native'
import Realm from 'realm';
import MyRow from './MyRow';
import { useStore } from '../state/state';
import { WordShema } from '../common/Shema';

const height=Dimensions.get('screen').height;
const ListComp = () =&gt; {
    
    const &#91;data, setData] = useState(&#91;])

    const loading = useStore(state =&gt; state.wordState)
    const search = useStore(state =&gt; state.search)
    const setLoading = useStore(state =&gt; state.setWordState)
    const setModalVisible = useStore(state =&gt; state.setPopupState)

    useEffect(() =&gt; {
        Realm.open({schema:&#91;WordShema]}).then(realm=&gt;{
            setData(realm.objects('Words').sorted('date',true))
        }).then(()=&gt;{setLoading(false)
            setModalVisible(false)})
    }, &#91;loading])
    useEffect(() =&gt; {
        Realm.open({schema:&#91;WordShema]}).then(realm=&gt;{
            setData(realm.objects('Words').sorted('date',true))
        }).then(()=&gt;{
            setModalVisible(false)
        })
    }, &#91;])
    useEffect(() =&gt; {
        Realm.open({schema:&#91;WordShema]}).then(realm=&gt;{
            setData(realm.objects('Words').filtered(
                'translate CONTAINS&#91;c] "'+search+'" OR name CONTAINS&#91;c] "'+search+'"'
            ))
        }).then(()=&gt;{setLoading(false)
            })
    }, &#91;search])
    return (
        &lt;View key='1' style={{height:height-145}}&gt;
            
        
           
            &lt;ScrollView overScrollMode="always"&gt;
                {data.length&gt;0&amp;&amp;
                
                data.map((data,ind)=&gt;{
                    return(
                        &lt;MyRow key={ind} data={data} ind={ind} /&gt;
                    )
                })
            }

            &lt;/ScrollView&gt;
            
        &lt;/View&gt;
    )
}

export default ListComp</code></pre>



<p>Bu komponentler içerisinde kullandığımız diğer komponentleri de burada paylaşabilirim.</p>



<pre class="wp-block-code"><code>//MyModal.js
import React from 'react'
import { StyleSheet, Modal, TouchableOpacity } from 'react-native'
import { useStore } from '../state/atom';
import SendComp from './SendComp';
import MyBlur from './MyBlur';
const MyModal = () =&gt; {

    const visible = useStore(state =&gt; state.popupState)
    const setVisible = useStore(state =&gt; state.setPopupState)
    const setSelectedData = useStore(state =&gt; state.setSelectedWordState)

    return (
        &lt;Modal
            animationType='slide'
            transparent={true}
            visible={visible}
        &gt;
            &lt;MyBlur/&gt;
            &lt;TouchableOpacity style={styles.view} onPress={()=&gt;{
                setVisible(false)
                setSelectedData({})}}&gt;
                &lt;SendComp isSend={false} /&gt;
            &lt;/TouchableOpacity&gt;
        &lt;/Modal&gt;
    )
}

export default MyModal

const styles = StyleSheet.create({
    view:{
        width:'100%',
        height:'100%',
        alignItems:'center',
        justifyContent:'center',
        alignSelf:'center',
        borderRadius:10
    }
})</code></pre>



<pre class="wp-block-code"><code>//MyRow.js
import React, {useEffect} from 'react'
import { Dimensions, StyleSheet, Text, View, TouchableOpacity } from 'react-native'
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
import { WordShema } from '../common/Shema';
import {  useStore } from '../state/atom';
import MyModal from './MyModal';

const width=Dimensions.get('screen').width;
const padding=20

Icon.loadFont()
const MyRow = ({data,ind}) =&gt; {
    const setModalVisible = useStore(state =&gt; state.setPopupState)

    const setLoading = useStore(state =&gt; state.setWordState)

    const selectedData = useStore(state =&gt; state.selectedWordState)
    const setSelectedData =useStore(state =&gt; state.setSelectedWordState)

    const deleter = ()=&gt;{
        Realm.open({schema:&#91;WordShema]}).then(realm=&gt;{
            realm.write(() =&gt; {
            let obj = realm.objects('Words').filtered("id='"+data.id+"'");
            realm.delete(obj)
            setLoading(true)

        })
        })
    }

    useEffect(() =&gt; {
        if(selectedData.name!=undefined){
            setModalVisible(true)
        }
    }, &#91;selectedData])
    return (
        &lt;TouchableOpacity key={ind} style={styles.container} onPress={()=&gt;setSelectedData(data)} &gt;
            &lt;MyModal/&gt;
            &lt;View style={styles.info} &gt;
                &lt;View style={styles.data}&gt;
                    &lt;Text style={styles.text}&gt;
                        {data.name}({data.pronunciation})
                    &lt;/Text&gt;
                    &lt;Text style={styles.text}&gt;
                        {data.translate}
                    &lt;/Text&gt;
                &lt;/View&gt;
                &lt;Text style={styles.description}&gt;
                    {data.description}
                &lt;/Text&gt;
            &lt;/View&gt;
            &lt;TouchableOpacity onPress={()=&gt;deleter()} style={styles.icon}&gt;
                &lt;Icon name="delete" size={30} color="red" /&gt;
            &lt;/TouchableOpacity&gt;
        &lt;/TouchableOpacity&gt;
    )
}

export default MyRow

const styles = StyleSheet.create({
    container:{
        
        padding:padding/2,
        width:width-padding,
        alignSelf:'center',
        margin:4,
        borderRadius:5,
        backgroundColor:'#ffffff60',
        flexDirection:'row',
        shadowColor:'black',
        shadowOpacity:0.2,
        shadowRadius:1,
        shadowOffset:{width: -1, height: 3},
    },
    info:{
        flex:1,
        justifyContent:'space-between'
    },
    data:{
        flexDirection:'row',
        justifyContent:'space-between'
        
    },
    text:{
        textAlign:'center',
        padding:2,
        color:'white',
        fontSize:20,
        fontWeight:'900',
        textShadowColor:'black',
        textShadowRadius:5,
        textShadowOffset:{width: 0, height: 0},
    },
    icon:{
        alignItems:'center',
        justifyContent:'center'
    },
    description:{
        color:'white',
        fontSize:14,
        fontWeight:'900',
        textShadowColor:'black',
        textShadowRadius:5,
        textShadowOffset:{width: 0, height: 0},
    }
})</code></pre>



<p>yes, we can use</p>



<pre class="wp-block-code"><code>npx react-native run-ios</code></pre>



<p> or</p>



<pre class="wp-block-code"><code>npx react-native run-android</code></pre>



<p>Tada it&#8217;s working now. Congratulations 🙂 </p>



<p>Forgive me if I made any mistakes as this is my first blog post 🙂</p>



<p>I will continue to add more such application development articles on mobile application development with React native, stay tuned 🙂 Stay healthy..</p>
]]></content:encoded>
					
					<wfw:commentRss>https://hanzala.com.tr/2021/07/25/react-native-realm-zustand-2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">436</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/


Served from: hanzala.com.tr @ 2026-04-17 03:20:52 by W3 Total Cache
-->