Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
why react does not run .map function?
import React, {Component} from 'react';
import {View, Text} from 'react-native';
import axios from 'axios'


//servislere bağlanabilmek için class haline getireceğiz
class Liste extends Component {
    state = {data: [] };
    UNSAFE_componentWillMount() {
        console.log('componentWillMount');
        axios.get('https://reqres.in/api/users')
        .then(response => this.setState({ data: response.data}));
    }
    componentDidMount() {
        console.log('componentDidMount');
    }

    renderData() {
        return this.state.data.map( responseData => 
            <Text> {responseData.title} </Text>    
    );
    }

    render() {
        console.log('gelen data', this.state);
        console.log('render');
        return (
            <View style={{marginTop: 5}}>
                {this.renderData()}
            </View>
        );
    }
}

export default Liste;

this is my Liste.js component script. i have an error that tell me "this.state.data.map is not a function". what should i do? what is wrong with me?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

EDIT :

As correctly pointed by @Bergi, we don't really need to bind the the renderData function, as the state is correctly available to the method. I took the liberty as created a fiddle to check the issues, and it is indeed with the response of the API call.

response.data returns an object but response.data.data is an Array. It is corrected in the fiddle

UNSAFE_componentWillMount() {
   console.log('componentWillMount');
   axios.get('https://reqres.in/api/users')
    .then(response => {  
         this.setState({ data: response.data.data})
     });
}


You need to bind the renderData to the correct lexical scope for react to be able to refer to your component's state defined within. Also, I'd recommend checking your response.data returned from the API call before setting the state.

Other option is to use arrow function renderData = () => {...} but it has a drawback that every instance of your component will get its own copy of this function, thereby increasing its memory footprint.

You can do the following:

import React, {Component} from 'react';
import {View, Text} from 'react-native';
import axios from 'axios'


//servislere bağlanabilmek için class haline getireceğiz
class Liste extends Component {
    constructor(props) {
      super(props);
      this.state = {data: [] };
      this.renderData = this.renderData.bind(this);
    }
    
    UNSAFE_componentWillMount() {
        console.log('componentWillMount');
        axios.get('https://reqres.in/api/users')
        .then(response => { 
            if(response && Array.isArray(response.data)) {
               this.setState({ data: response.data})
            } else {
              // handle the type mismatch here
            }
         });
    }
    componentDidMount() {
        console.log('componentDidMount');
    }

    renderData() {
        return this.state.data.map( responseData => 
            <Text> {responseData.title} </Text>    
        );
    }

    render() {
        console.log('gelen data', this.state);
        console.log('render');
        return (
            <View style={{marginTop: 5}}>
                {this.renderData()}
            </View>
        );
    }
}

export default Liste;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!