What I have Done for that
class MainPostPoll extends Component<any, any> { constructor(props: any) { super(props) this.state = { loading: false, fromFetch: false, fromAxios: false, axiosData: [], } }
goForAxios = () => { this.setState({ loading: true, }) axios .get('https://hn.algolia.com/api/v1/search_by_date?tags=story&page=0') .then((response) => { console.log('getting data from axios', response.data)
this.setState({
loading: false,
axiosData: response.data.hits,
})
})
.catch((error) => {
console.log(error)
})
}
renderItem = (item: any) => { return ( {item.item.title} {item.item.url} {item.item.created_at} {item.item.author} ) }
render() { return (
<View>
<View style={{ margin: 18 }}>
<TouchableOpacity style={{}} onPress={this.goForAxios}>
<Text>PostPoll</Text>
</TouchableOpacity>
</View>
<FlatList
data={this.state.axiosData}
renderItem={(item) => this.renderItem(item)}
keyExtractor={(item) => item.id}
/>
</View>
)
} }