Answer the question
In order to leave comments, you need to log in
Why are states not saved in React?
I get an array via the API, save it in states and try to render it to the form, there are no errors, but the screen is empty, when I simply access the array, it explicitly indicates all the keys to me, when accessing the keys, it displays emptiness.
import React, {Component} from 'react';
import { StyleSheet, Text, View, Image, Button } from 'react-native';
const myObj = {
name: '',
age: '',
favoriteFood: ''
};
const myObjStr = JSON.stringify(myObj);
export default class App extends React.Component {
state = {
data: ''
}
componentDidMount = () => {
fetch('https://delovoe-tv.ru/wp-json/wp/v2/posts/', {
method: 'GET'
})
.then((response) => response.json())
.then((responseJson) => {
global.SampleVar = JSON.stringify(responseJson);
global.Var = JSON.parse(SampleVar);
Var.forEach(function(item, i, arr) {
var arr = new Array();
arr.push(i,item['title']['rendered']);
//alert( i + ": " + item['title']['rendered'] + " (массив:" + arr + ")" );
global.arr;
});
this.setState({
data: responseJson
})
})
.catch((error) => {
console.error(error);
});
}
render() {
const handlePress = () => false
return (
<View style={styles.container}>
<Image
style={{width: 300, height: 100,paddingTop:0,marginTop:0}}
source={{uri: 'http://delovoe-tv.ru/wp-content/uploads/2018/07/DTV_Logo.png'}}
/>
<Text style={{width: 250, paddingTop:30}}>
{this.state.data['title']}
</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
top:50,
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'flex-start',
},
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question