代码之家  ›  专栏  ›  技术社区  ›  konuralpt

从异步存储中响应本机返回视图

  •  0
  • konuralpt  · 技术社区  · 6 年前

    我正在尝试从AsyncStorage呈现卡列表。我要做的是从存储中获取所有数据,并根据这些数据创建卡列表。我可以在函数中看到存储数据,但不能返回视图。我就是这么做的。

    import {View,AsyncStorage} from "react-native";
    import { Container, Header, Content, Card, CardItem, Text, Body, Icon, Fab } 
    from "native-base";
    
    
    export default class extends Component{
    
     async _retrieveData(){
        const value = await AsyncStorage.getAllKeys();
        return value;
     };
    
     getAll(){
       this._retrieveData()
         .then(items =>{
           items.map(async (k) => {
             await AsyncStorage.getItem(k).then(ok => {
    
               var cards = [];
    
                 cards.push(
    
                   <Card key="">
                     <CardItem>
                       <Body>
                         <Text>
                           ok data
                         </Text>
                       </Body>
                     </CardItem>
                   </Card>
    
                 )
               return cards;
    
             });
           });
    
         })
         .catch(err =>{
           alert(err);
         });
       };
    
    
       render(){
    
           return(
             <Container>
               <Content padder>
               {this.getAll()}
               </Content>
             </Container>
           )
       }
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Luka Dumančić    6 年前

    不能在render方法内调用getall函数,因为它需要promise来解决。

    您可以在componendIDmount方法中调用相同的函数,将卡保存到本地状态,然后在render方法中呈现该数据。