Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

198
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda