类型错误:未定义不是对象(评估“this.state”)

时间:2021-02-13 20:19:49

标签: javascript reactjs react-native

我看到有很多类似的问题,但没有一个答案对我有帮助。

运行以下代码时出现此错误:TypeError: undefined is not an object (evaluating 'this.state') 我也收到此错误,但这与 this.setState(): TypeError: _this.setState is not a function 有关。 (在 '_this.setState({ 检查: !_this.state.checked })', '_this.setState' 未定义)

READ  address:   80 2e   data:   20   JSR   ; Read the opcode
READ  address:   80 2f   data:   14   14    ; Read the LO byte of operand
READ  address:   01 b6   data:   74   't'   ; Read the stack (why?!)
WRITE address:   01 b6   data:   80   ---   ; Write HI byte of PC
WRITE address:   01 b5   data:   30   '0'   ; Write LO byte of PC
READ  address:   80 30   data:   80   80    ; Read HI byte of operand
READ  address:   80 14   data:   8d   STA   ; Read opcode at `80 14` (the JSR operand)

完整代码(如有必要):

<CheckBox
   center
   title='Click Here'
   checked={this.state.checked}
   onPress={() => this.setState({checked: !this.state.checked})}
/>

1 个答案:

答案 0 :(得分:1)

您正在函数组件中使用 this.setState。您不能将这两者混合在一起(您只能在类组件中使用它)。如果要使用 checked,则需要添加 const [checked, setChecked] = useState(false); 并使用 setChecked 设置已选中的状态。 titledescription 也是如此。

相关问题