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?
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 Also, I'd recommend checking your bind
the renderData
to the correct lexical scope for react to be able to refer to your component's state defined within.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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.