TypeError:TypeError:null不是对象(正在评估'this.state.ischecked1')

时间:2019-02-08 06:04:03

标签: react-native react-native-android expo

每次尝试单击复选框时,我都会看到上述错误 我希望对下面显示的四个复选框进行单独检查,但是我不能这样做。请帮忙! 代码如下:

import * as React from 'react';
import {
  Text,
  View,
  StyleSheet,
  TouchableOpacity,
  ScrollView,
  Button,
} from 'react-native';
import { Header, Icon } from 'react-native-elements';
import { Constants } from 'expo';
import { CheckBox } from 'react-native-elements';

import {
  createStackNavigator,
  createNavigatorContainer,
} from 'react-navigation';


export default class UpdateTypes extends React.Component {
  static navigationOptions = {
    title: 'UPDATE TYPES',
    style: {
      display: 'flex',
      flexDirection: 'row',
      backgroundColor: '#FFD700',
    },
  };

  constructor(props) {
        super(props);     

        this.state = {
            ischecked1: true,
            ischecked2: false,
            ischecked3: true,
            ischecked4: false,
        } 
    }  


  onChangeCheck1() {
    this.setState({ ischecked1: !this.state.ischecked1 });
  }
  onChangeCheck2() {
    this.setState({ ischecked2: !this.state.ischecked2 });
  }
  onChangeCheck3() {
    this.setState({ ischecked3: !this.state.ischecked3 });
  }
  onChangeCheck4() {
    this.setState({ ischecked4: !this.state.ischecked4 });
  }

  render() {

    return (
      <View style={styles.container}>
        <View style={styles.innerview}>
          <TouchableOpacity style={styles.options}>
            <Text>Select All</Text>
          </TouchableOpacity>
          <TouchableOpacity style={styles.options}>
            <Text>Deselect All</Text>
          </TouchableOpacity>
        </View>

        <Text style={styles.writeup}>
          Some random Texts. Below are some checkboxes. Bla Bla Bla!
        </Text>

        <View style={StyleSheet.create({ flex: 1 })}>
          <CheckBox
            center
            title="CheckBoxA"
            checkedIcon="dot-circle-o"
            uncheckedIcon="circle-o"
            value={this.state.ischecked1}
            checked={this.state.ischecked1}
            onPress={this.onChangeCheck1}
          />

          <CheckBox
            center
            title="CheckBoxB"
            checkedIcon="dot-circle-o"
            uncheckedIcon="circle-o"
            checked={this.state.ischecked2}
            value={this.state.ischecked2}
            onPress={this.onChangeCheck2}
          />

          <CheckBox
            center
            title="CheckBoxC"
            checkedIcon="dot-circle-o"
            uncheckedIcon="circle-o"
            checked={this.state.ischecked3}
            value={this.state.ischecked3}
            onPress={this.onChangeCheck3}
          />

          <CheckBox
            center
            title="CheckBoxD"
            checkedIcon="dot-circle-o"
            uncheckedIcon="circle-o"
            value={this.state.ischecked4}
            checked={this.state.ischecked4}
            onPress={this.onChangeCheck4}

          />
        </View>

        <TouchableOpacity style={styles.adsense}>
          <Text>Adsense Ad</Text>
        </TouchableOpacity>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    display: 'flex',
    flexDirection: 'column',
  },
  options: {
    flex: 1,
    alignItems: 'center',
    borderColor: '#008080',
  },
  writeup: {
    flex: 0.3,
    backgroundColor: '#FFE4B5',
    justifyContent: 'center',
    alignItems: 'center',
    padding: 8,
  },
  innerview: {
    flex: 0.4,
    display: 'flex',
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'center',
  },
  adsense: {
    flex: 0.55,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#778899',
  },
});

我也很困惑何时使用:

  • onChange
  • onChangeCheck
  • onIconPress
  • onLongPress
  • onPress

这些相同还是有特定用法?

2 个答案:

答案 0 :(得分:1)

一件事显然是错误的:将const state =替换为this.state=

另外,绑定功能:

onPress={this.onChangeCheck1.bind(this)}

更新的代码(您可以使用与'selectAll'类似的方式添加'deselectAll'):

import * as React from 'react';
import {
  Text,
  View,
  StyleSheet,
  TouchableOpacity
} from 'react-native';
import { CheckBox } from 'react-native-elements';


export default class UpdateTypes extends React.Component {
  static navigationOptions = {
    title: 'UPDATE TYPES',
    style: {
      display: 'flex',
      flexDirection: 'row',
      backgroundColor: '#FFD700'
    }
  };

  constructor(props) {
        super(props);

        this.state = {
            ischecked1: true,
            ischecked2: false,
            ischecked3: true,
            ischecked4: false
        };
    }


  onChangeCheck1() {
    this.setState({ ischecked1: !this.state.ischecked1 });
  }
  onChangeCheck2() {
    this.setState({ ischecked2: !this.state.ischecked2 });
  }
  onChangeCheck3() {
    this.setState({ ischecked3: !this.state.ischecked3 });
  }
  onChangeCheck4() {
    this.setState({ ischecked4: !this.state.ischecked4 });
  }

  selectAll() {
    this.setState({
      ischecked1: true,
      ischecked2: true,
      ischecked3: true,
      ischecked4: true
    });
  }

  render() {

    return (
      <View style={styles.container}>
        <View style={styles.innerview}>
          <TouchableOpacity style={styles.options} onPress={this.selectAll.bind(this)}>
            <Text>Select All</Text>
          </TouchableOpacity>
          <TouchableOpacity style={styles.options}>
            <Text>Deselect All</Text>
          </TouchableOpacity>
        </View>

        <Text style={styles.writeup}>
          Some random Texts. Below are some checkboxes. Bla Bla Bla!
        </Text>

        <View style={StyleSheet.create({ flex: 1 })}>
          <CheckBox
            center
            title="CheckBoxA"
            checkedIcon="dot-circle-o"
            uncheckedIcon="circle-o"
            value={this.state.ischecked1}
            checked={this.state.ischecked1}
            onPress={this.onChangeCheck1.bind(this)}
          />

          <CheckBox
            center
            title="CheckBoxB"
            checkedIcon="dot-circle-o"
            uncheckedIcon="circle-o"
            checked={this.state.ischecked2}
            value={this.state.ischecked2}
            onPress={this.onChangeCheck2.bind(this)}
          />

          <CheckBox
            center
            title="CheckBoxC"
            checkedIcon="dot-circle-o"
            uncheckedIcon="circle-o"
            checked={this.state.ischecked3}
            value={this.state.ischecked3}
            onPress={this.onChangeCheck3.bind(this)}
          />

          <CheckBox
            center
            title="CheckBoxD"
            checkedIcon="dot-circle-o"
            uncheckedIcon="circle-o"
            value={this.state.ischecked4}
            checked={this.state.ischecked4}
            onPress={this.onChangeCheck4.bind(this)}

          />
        </View>

        <TouchableOpacity style={styles.adsense}>
          <Text>Adsense Ad</Text>
        </TouchableOpacity>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    display: 'flex',
    flexDirection: 'column'
  },
  options: {
    flex: 1,
    alignItems: 'center',
    borderColor: '#008080'
  },
  writeup: {
    flex: 0.3,
    backgroundColor: '#FFE4B5',
    justifyContent: 'center',
    alignItems: 'center',
    padding: 8
  },
  innerview: {
    flex: 0.4,
    display: 'flex',
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'center'
  },
  adsense: {
    flex: 0.55,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#778899'
  }
});

答案 1 :(得分:0)

您需要对复选框的onClick事件使用自动绑定或箭头功能。

    onChangeCheck1 = () => {
    this.setState({ ischecked1: !this.state.ischecked1 });
    }

它没有得到'this'对象,因为调用onClick的上下文是不同的。