KeykoYume KeykoYume - 1 year ago 213
Javascript Question

React Native Changing Variable with State

I'm new to react and I just started playing around with state and props.
What I have been trying is to fetch some data and then populate with it some card views.

This is how my code looks so far:

class Two extends React.Component {
constructor(props) {
this.state = {
day: "Monday",

// Getting monthly schedule
getSchedules = () => {
fetch("http://xxxxxx/getschedules.php", {method: "GET"})
.then((response) => response.json())
.then((responseData) => {
"GET Response",
"Search Query -> " + responseData.result.length)
this.setState({day: responseData.result[1].day}); //changing state but nothing changes
.then((data) => {
.catch(function(err) {

render() {
<TouchableOpacity onPress={this.getSchedules}>
<View style={styles.card}>

If I click on the card the text value is supposed to change but nothing happens. Also is there a way to change the state automatically when the page loads without having to click on the card?
Any help would be greatly appreciated!

Answer Source

In your onPress code: <TouchableOpacity onPress={this.getSchedules}>

It should be <TouchableOpacity onPress={this.getSchedules.bind(this)}>

By only passing function reference this context isn't passed to getSchedules.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download