TypeError: TypeError: null 不是对象(计算 'this.state.ischecked1')


每次

尝试单击复选框时,我都会看到上述错误我想对下面显示的四个复选框进行单独检查,但我无法做到这一点。请帮忙!代码如下:

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

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

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

此外,绑定函数:

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

更新了代码(您可以以与"selectAll"类似的方式添加"取消选择全部"(:

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'
  }
});

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

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

它没有获取"this"对象,因为调用 onClick 的上下文不同。

相关内容

  • 没有找到相关文章