React Native
...
Users
Mengambil Pengguna Saat Ini di Parse dengan React Native
7 mnt
dapatkan pengguna saat ini untuk react native pendahuluan setelah menerapkan pendaftaran pengguna dan login ke aplikasi anda, anda perlu mengambil data pengguna yang sedang login saat ini untuk melakukan berbagai tindakan dan permintaan karena react native menggunakan asyncstorage asyncstorage sebagai penyimpanan lokal, data ini dapat diambil menggunakan parse currentasync parse currentasync di dalam komponen aplikasi anda prasyarat untuk menyelesaikan tutorial ini, anda akan membutuhkan aplikasi react native yang dibuat dan terhubung ke back4app selesaikan panduan sebelumnya agar anda dapat memiliki pemahaman yang lebih baik tentang kelas pengguna parse tujuan ambil data pengguna saat ini dengan menggunakan parse untuk aplikasi react native 1 mengambil pengguna saat ini metode parse currentasync parse currentasync dapat digunakan di mana saja dalam kode anda, setelah mengonfigurasi aplikasi anda dengan benar untuk menggunakan parse dan asyncstorage responsnya akan menjadi objek pengguna anda saat ini ( parse user parse user ) atau null jika tidak ada pengguna yang masuk saat ini javascript 1 const getcurrentuser = async function () { 2 const currentuser = await parse user currentasync(); 3 if (currentuser !== null) { 4 alert alert( 5 'success!', 6 `${currentuser get('username')} is the current user!`, 7 ); 8 } 9 return currentuser; 10 };1 const getcurrentuser = async function () promise\<parse user> { 2 const currentuser parse user = await parse user currentasync(); 3 if (currentuser !== null) { 4 alert alert( 5 'success!', 6 `${currentuser get('username')} is the current user!`, 7 ); 8 } 9 return currentuser; 10 }; metode ini sangat penting dalam situasi di mana anda tidak memiliki akses ke status aplikasi anda atau data pengguna anda, sehingga memungkinkan untuk melakukan permintaan parse yang relevan di komponen mana pun dari aplikasi anda 2 menggunakan pengguna saat ini dalam komponen react native dalam panduan kami sebelumnya, parse currentasync parse currentasync sudah digunakan untuk pengujian dan di dalam hellouser hellouser komponen berikut adalah komponen lengkapnya lagi hellouser js 1 import react, {fc, reactelement, useeffect, usestate} from 'react'; 2 import {text, view} from 'react native'; 3 import parse from 'parse/react native'; 4 import styles from ' /styles'; 5	 6 export const hellouser = () => { 7 // state variable that will hold username value 8 const \[username, setusername] = usestate(''); 9	 10 // useeffect is called after the component is initially rendered and 11 // after every other render 12 useeffect(() => { 13 // since the async method parse user currentasync is needed to 14 // retrieve the current user data, you need to declare an async 15 // function here and call it afterwards 16 async function getcurrentuser() { 17 // this condition ensures that username is updated only if needed 18 if (username === '') { 19 const currentuser = await parse user currentasync(); 20 if (currentuser !== null) { 21 setusername(currentuser getusername()); 22 } 23 } 24 } 25 getcurrentuser(); 26 }, \[username]); 27	 28 // note the conditional operator here, so the "hello" text is only 29 // rendered if there is an username value 30 return ( 31 \<view style={styles login wrapper}> 32 \<view style={styles form}> 33 {username !== '' && \<text>{`hello ${username}!`}\</text>} 34 \</view> 35 \</view> 36 ); 37 }; hellouser tsx 1 import react, {fc, reactelement, useeffect, usestate} from 'react'; 2 import {text, view} from 'react native'; 3 import parse from 'parse/react native'; 4 import styles from ' /styles'; 5	 6 export const hellouser fc<{}> = ({}) reactelement => { 7 // state variable that will hold username value 8 const \[username, setusername] = usestate(''); 9	 10 // useeffect is called after the component is initially rendered and 11 // after every other render 12 useeffect(() => { 13 // since the async method parse user currentasync is needed to 14 // retrieve the current user data, you need to declare an async 15 // function here and call it afterwards 16 async function getcurrentuser() { 17 // this condition ensures that username is updated only if needed 18 if (username === '') { 19 const currentuser = await parse user currentasync(); 20 if (currentuser !== null) { 21 setusername(currentuser getusername()); 22 } 23 } 24 } 25 getcurrentuser(); 26 }, \[username]); 27	 28 // note the conditional operator here, so the "hello" text is only 29 // rendered if there is an username value 30 return ( 31 \<view style={styles login wrapper}> 32 \<view style={styles form}> 33 {username !== '' && \<text>{`hello ${username}!`}\</text>} 34 \</view> 35 \</view> 36 ); 37 }; dalam hal ini, metode parse currentasync parse currentasync mengambil username dan memperbarui variabel state yang dirender di dalam jsx komponen kesimpulan di akhir panduan ini, anda telah belajar bagaimana cara mengambil data pengguna parse saat ini dari penyimpanan lokal di react native di panduan berikutnya, kami akan menunjukkan kepada anda bagaimana cara memungkinkan pengguna anda untuk mengatur ulang kata sandinya