最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - render function not rendering anything React Native - Stack Overflow

programmeradmin0浏览0评论

Nothing is rendering on screen. No clue why its happening, No error anywhere. Wondering why nothing is showing on screen. Getting data from API properly. Code is given below:

import React, { Component } from 'react';
import { StyleSheet, ScrollView, TouchableOpacity, Text, View } from 'react-native';
import { Container, Content, Grid, Row, Col } from 'native-base';
import axios from 'axios';
import ItemCard  from '../ponents/ItemCard';

export default class ItemHorizontalScreen  extends Component {
    constructor(props) {
        super(props)
        this.state = {
            items: []
        }
    }

    ponentWillMount() {
        axios.get('').then(response => 
            this.setState({
                items: response.data
            }))
        .catch((error) => {
            console.error(error);
        })
    }

    renderHorizontalContents() {
        const rowItems = this.state.items
        rowItems.map((rowItem, index) => {
            return (
                <TouchableOpacity key={index}>
                    <Text>{rowItem.title}</Text>
                </TouchableOpacity>
            )
        })
    } 

    render() {
        return (
            <View>
            {this.renderHorizontalContents()}
            </View>
        )
    }
}

Nothing is rendering on screen. No clue why its happening, No error anywhere. Wondering why nothing is showing on screen. Getting data from API properly. Code is given below:

import React, { Component } from 'react';
import { StyleSheet, ScrollView, TouchableOpacity, Text, View } from 'react-native';
import { Container, Content, Grid, Row, Col } from 'native-base';
import axios from 'axios';
import ItemCard  from '../ponents/ItemCard';

export default class ItemHorizontalScreen  extends Component {
    constructor(props) {
        super(props)
        this.state = {
            items: []
        }
    }

    ponentWillMount() {
        axios.get('http://rallycoding.herokuapp./api/music_albums').then(response => 
            this.setState({
                items: response.data
            }))
        .catch((error) => {
            console.error(error);
        })
    }

    renderHorizontalContents() {
        const rowItems = this.state.items
        rowItems.map((rowItem, index) => {
            return (
                <TouchableOpacity key={index}>
                    <Text>{rowItem.title}</Text>
                </TouchableOpacity>
            )
        })
    } 

    render() {
        return (
            <View>
            {this.renderHorizontalContents()}
            </View>
        )
    }
}
Share Improve this question asked Oct 16, 2018 at 13:36 Wahidul AlamWahidul Alam 1,2535 gold badges31 silver badges60 bronze badges 2
  • 1 you have to return something from your renderHorizontalContents function. use return rowItems.map((rowItem, index) => {...} – Anurag Awasthi Commented Oct 16, 2018 at 13:37
  • good catch.. working now .. thanks a lot :) @nrgwsth – Wahidul Alam Commented Oct 16, 2018 at 13:39
Add a ment  | 

2 Answers 2

Reset to default 3

Your renderHorizontalContents() should return the list:

renderHorizontalContents() {
    const rowItems = this.state.items
    return rowItems.map((rowItem, index) => {
        return (
            <TouchableOpacity key={index}>
                <Text>{rowItem.title}</Text>
            </TouchableOpacity>
        )
    })
} 

Also, semi-related, as of React 16.3 React team advises not to use ponentWillMount(). You should fetch the data in ponentDidMount() LifeCycle hook.

More on ponentWillMount() deprecation:

https://github./styled-ponents/styled-ponents/issues/1575

Try this. The issue is renderHorizontalContents() is not returning so you have to return elements which map returns for you

Also regarding adding key, if items array contains unique id per object then use that is as key that is remended. Index is always second option if you don’t have unique id from data. Also when adding index as key you should add something like I did below instead of adding index as key directly.

renderHorizontalContents() {
        const { items } = this.state;
           return items.map((rowItem, index) => {
            return (
                <TouchableOpacity key={"Key"+index}>
                    <Text>{rowItem.title}</Text>
                </TouchableOpacity>
            )
        })
    } 
发布评论

评论列表(0)

  1. 暂无评论