Is it possible to dynamically create components in React Native?

后端 未结 5 559
萌比男神i
萌比男神i 2021-02-04 06:40

Is it possible to dynamically create and add view components in React Native? For example, firstly I have only empty screen and information of all views come from server in JSO

5条回答
  •  再見小時候
    2021-02-04 06:57

    Yes this is possible. Assume that you retrieve your JSON data successfully and save it to some state then in your render function you can use it like that;

    render() {
        var productList = [];
    
            this.state.data.products.forEach(function (tmpProduct) {
                productList.push(
                    
    
                        
                            
                                
                            
                            
                                {tmpProduct.title}
                                {tmpProduct.description}
                            
                        
                    
                );
            }.bind(this));
    
        return (
            
                
    
                    
    
                        {productList}
    
                    
    
                
            
        )
    }
    

    I hope this code piece give you an idea. You can adapt it to your case.

提交回复
热议问题