1

I'm trying to a implement a logic to display some data fetched from a simple REST API. So i'm grabbing the JSON object in the RanjoorExplore class and the data is sent into the data to the ExploreCard in the other class. So the this.props.data must be referencing the passed variable. By mapping that variable, I'm displaying the title attribute of the response object in a simple Text Component. I'm facing this error:

undefined is not a function(evaluating this.props.data.map).

RanjoorExplore:

    import React, { Component } from 'react';
    import {
        StyleSheet,
        Text,
        View,
        Image,
        ScrollView,
        Alert
    } from 'react-native';
    import Icon from 'react-native-vector-icons/FontAwesome';
    import ExploreCard from '../../elements/cards/ExploreCard';
    import ExploreHeader from '../../elements/headers/ExploreHeader';

    class RanjoorExplore extends Component {

        constructor(){
            super();
            this.state = {
                rawData: []
            }
        }

        static navigationOptions = {
            header: null,
            title: 'Explore',
            tabBarIcon: ({ tintColor, focused }) => (
                <Icon
                    name="bandcamp"
                    size={24}
                    color={focused ? '#4ab367' : 'white'}
                />
            ),
            headerStyle: { backgroundColor: '#202026' },
            headerTitleStyle: {
                color: 'white'
            }
        };

        fetchGanjoorData() {
            return fetch('https://jsonplaceholder.typicode.com/posts/1')
                .then((response) => response.json())
                .then((responseJson) => {
                    this.setState({rawData: responseJson})
                })
                .catch((error) => {
                    console.error(error);
                });
        }

        componentDidMount() {
            this.fetchGanjoorData();
        }

        render() {
            return (
                <View style={styles.ExploreContainer}>
                    <ExploreHeader />
                    <ScrollView>              
                        <ExploreCard data={this.state.rawData} />
                    </ScrollView>
                </View>
            );
        }
    }

    var styles = StyleSheet.create({
        ExploreContainer: {
            backgroundColor: '#303036',
            height: '100%',
            width: '100%'
        },
    })
    export default RanjoorExplore

ExploreCard:

    import React, { Component } from 'react';
    import {
        StyleSheet,
        Text,
        View,
        Image,
        Alert
    } from 'react-native';
    import { Card, ListItem, Button, Icon, Avatar } from 'react-native-elements';

    export default class ExploreCard extends Component {
        render() {
            /* Mapped data will be processed right here */
            let mappedData = this.props.data.map(function (data1) {
                return (
                    <View>
                        {data1.title}
                    </View>

                )
            })

            return (
                <View style={{ flexDirection: 'row' }}>
                    <View style={{ flex: 1 }}></View>
                    <Card
                        containerStyle={{
                            width: '85%', height: 250, backgroundColor: '#202026', shadowOpacity: 0.7,
                            shadowOffset: { height: 5 }, shadowColor: 'black', borderWidth: 0, borderRadius: 8, flexDirection: 'row'
                        }}
                        wrapperStyle={{ alignSelf: 'flex-end' }} >

                        <View style={{ flex: 2, alignSelf: 'flex-end' }}>
                            <View style={{ flexDirection: 'row', alignSelf: 'flex-end' }}>
                                <Text style={{ fontFamily: 'IRANSans', marginRight: 5, marginTop: 12, color: '#505056' }}>حافظ</Text>
                                <Avatar
                                    medium
                                    rounded
                                    source={require('../../img/avatars/ferdowsi.jpg')}
                                    containerStyle={{
                                        alignSelf: 'flex-end', marginRight: 15,
                                        shadowOpacity: 0.7,
                                        shadowOffset: { height: 5 }, shadowColor: 'black'
                                    }}
                                />
                            </View>

                            <View>
                                <Text style={{ alignSelf: 'flex-end', fontFamily: 'IRANSans', color: 'white', marginTop: '10%', marginRight: '5%' }}>
                                    {mappedData}
                                </Text>
                                <Text style={{ alignSelf: 'flex-start', fontFamily: 'IRANSans', color: 'white' }}>
                                    تا دمی برآساییم زین حجاب ظلمانی
                            </Text>
                            </View>
                        </View>
                        <View style={{ alignSelf: 'flex-end', backgroundColor: 'transparent', flexDirection: 'row' }}>
                            <Icon
                                name='favorite' size={24} color="#34343a" style={{ marginLeft: 5 }}
                            />
                            <Icon
                                name='grade' size={24} color="#34343a" style={{ marginLeft: 5 }}
                            />
                            <View>
                                <Button
                                    textStyle={{ fontSize: 15 }}
                                    iconRight
                                    backgroundColor='#4ab367'
                                    fontFamily='IRANSans_UltraLight'
                                    buttonStyle={{
                                        height: 15, width: 110,
                                        borderRadius: 8
                                    }}
                                    title='ادامه مطلب'
                                />

                            </View>

                        </View>
                    </Card>
                    <View style={{ flex: 1 }}></View>
                </View>
            );
        }
    }

Can someone please help me solve this issue? Thanks in advance.

3
  • 1
    You should add condition for data not to be blank before calling map over it Commented Jul 14, 2017 at 17:35
  • 1
    my guess is that responseJson is not an array like you think it is Commented Jul 14, 2017 at 18:37
  • @GarrettMcCullough Yeah right, But i thought the structure of the map function is similar to the for loop. It iterates through 1 object only in case needed. I found out that the response must be array not object. Thanks anyway. Commented Jul 15, 2017 at 5:10

1 Answer 1

1

https://jsonplaceholder.typicode.com/posts/1 returns an object, not an array. Therefore, map is not a valid operation

Perhaps you meant to use https://jsonplaceholder.typicode.com/posts/? That returns an array

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.