Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

199
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda