I'm trying to build a simple app that takes a json containing a list of names with their corresponding 1 or 0 "active" state and displays them on a listview representing the "active" state using a switch.
I went through the tutorial and came up with this:
import React, { Component } from 'react';
import {
AppRegistry,
Switch,
Text,
View,
ListView
} from 'react-native';
import data from './test-users.json';
// this contains:
// name: a string containing the name
// active: the state of the switch, either 1 or 0.
const Row = (props) => (
<View>
<Text>
{`${props.name}`}
</Text>
<Switch value={props.active == 1}
onValueChange={(value) => this.setState(value)} />
</View>
);
class ListViewTest extends React.Component {
constructor(props) {
super(props);
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
dataSource: ds.cloneWithRows(data),
};
}
render() {
return (
<ListView
dataSource={this.state.dataSource}
renderRow={(data) => <Row {...data} />}
/>
);
}
}
export default ListViewTest;
AppRegistry.registerComponent('ListViewTest', () => ListViewTest);
This displays fine, but when I try to click on a switch I get the following error stating:
undefined is not a function (evaluating '_this.setState(value)')
I feel like I must be misunderstanding something rather fundamental about how react-native works, but I can't quite figure out what it is. Is setState not the way to change state?