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

Nothing is rendering on screen. No clue why its happening, No error anywhere. Wondering why nothing is

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>
            )
        })
    } 

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745637221a4637467.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信