TypeError: undefined is not an object in

我对 Javascript 和 ReactJS 真的很陌生,并且对错误感到困惑TypeError: undefined is not an object (evaluating '_this.state = { destination: '' }')


这是我的代码:


import React, {Component} from 'react';

import { StyleSheet, View, TextInput } from 'react-native';


export default class SearchBar extends Component {

    constructor() {

        /*

        this.state = {

            destination: ''

        };

        */

        super();

    }

    render() {

        return (

            <View style={styles.inputContainer}>

                <TextInput

                    style={styles.input}

                />

            </View>

        );

    }

}

这段代码工作正常。但是一旦我取消评论


this.state = {

            destination: ''

        };

我得到了错误。为什么它在困扰我undefined?我没有在任何地方访问它。


这可能看起来像这里问题的重复, 但不同之处在于,在该问题中,解决方案涉及.bind(this)在提问者定义的函数上使用。我没有这样的功能,我可以调用它。


任何帮助将不胜感激。


慕码人2483693
浏览 273回答 1
1回答

慕的地10843

this由 配置super。执行之前super,this尚未配置。这就是您遇到问题的原因。使固定:constructor(props) {&nbsp; &nbsp; super(props);&nbsp; &nbsp; this.state = {&nbsp; &nbsp; &nbsp; &nbsp; destination: ''&nbsp; &nbsp; };}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript