Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
Looping return from AsyncStorage

i have following code returning value from AsyncStorage

const getData = async () => {
        try {
            let listlayana = JSON.parse(await AsyncStorage.getItem('layanan'));
            return listlayana
        }catch (e){

        }
    }
    const datas = getData()

and i trying to loop this function result with this

{
                datas.map((item, i) => (
                    <TouchableOpacity key={i}>
                        <View key={i} style={styles.listitem}>
                            <Text style={styles.listtext} >{item.nmlayanan}</Text>
                            <Text style={[styles.listtext, {color: "#b4b3b3"}]}> > </Text>
                        </View>
                    </TouchableOpacity>
                ))
            }

and i got this following error

undefined is not a function (near '...datas.map...')
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use React.useEffect and React.useState for this target like this:

const [data, setData] = React.useState(); //add this useState for the data

const getData = async () => {
  try {
    let listlayana = JSON.parse(await AsyncStorage.getItem("layanan"));
    return listlayana;
  } catch (e) {}
};
  
// and add this async useeffect for fetching data from asyncstorage
React.useEffect(async () => {
  const scopeData = await getData();
  setData(scopeData);
}, []);

and then your render function like this:

{data.map((item, i) => (
  <TouchableOpacity key={i}>
    <View key={i} style={styles.listitem}>
      <Text style={styles.listtext}>{item.nmlayanan}</Text>
      <Text style={[styles.listtext, { color: "#b4b3b3" }]}> > </Text>
    </View>
  </TouchableOpacity>
))}

and your component like this:

export default function SOF() {
  const [data, setData] = React.useState();

  const getData = async () => {
    try {
      let listlayana = JSON.parse(await AsyncStorage.getItem("layanan"));
      return listlayana;
    } catch (e) {}
  };

  React.useEffect(async () => {
    const scopeData = await getData();
    setData(scopeData);
  });

  return (
    <View>
      {data.map((item, i) => (
        <TouchableOpacity key={i}>
          <View key={i} style={styles.listitem}>
            <Text style={styles.listtext}>{item.nmlayanan}</Text>
            <Text style={[styles.listtext, { color: "#b4b3b3" }]}> > </Text>
          </View>
        </TouchableOpacity>
      ))}
    </View>
  );
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!