简体   繁体   English

TypeError: undefined is not an object(评估“this.state”)

[英]TypeError: undefined is not an object (evaluating 'this.state')

I can see there's a lot of similar questions, but none of the answer could help me.我可以看到有很多类似的问题,但没有一个答案可以帮助我。

I get this error when running the code below: TypeError: undefined is not an object (evaluating 'this.state') I also get this error, but this is connected to this.setState(): TypeError: _this.setState is not a function.运行以下代码时出现此错误: TypeError: undefined is not an object (evalating 'this.state') 我也收到此错误,但这是连接到 this.setState(): TypeError: _this.setState is not an function。 (In '_this.setState({ checked: ._this.state,checked })'. '_this.setState' is undefined) (在'_this.setState({检查:._this.state,检查})'。'_this.setState'未定义)

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

The whole code (if necessary):整个代码(如有必要):

export default function NewEventScreen() {

    const [date, setDate] = useState(new Date());
    const [mode, setMode] = useState('date');
    const [show, setShow] = useState(false);

    const onChange = (event, selectedDate) => {
        const currentDate = selectedDate || date;
        setShow(Platform.OS === 'ios');
        setDate(currentDate);
      };
    
      const showMode = (currentMode) => {
        setShow(true);
        setMode(currentMode);
      };
    
      const showDatepicker = () => {
        showMode('date');
      };
    
      const showTimepicker = () => {
        showMode('time');
      };

    handleEventCreation = () => {
        const { title, description } = this.state
        firebase.firestore()
             .collection('users').doc(firebase.auth().currentUser.uid).collection('events').doc().set({
                    title: title,
                    description: description,
                }).then(() => { console.log("Document written") 
                    this.setState({
                        title: '',
                        description: '',
                    })
                }).catch(error => console.log(error))
    }

    state = {
        title: '',
        description: '',
        checked: false,
    }

    onPress = () => {
        this.setState({checked: !this.state.checked})
    }

        return (
            <View style={styles.container}>
                <Text style={styles.title}>Here can you make your events!</Text>
                <TextInput
                    style={styles.inputBox}
                    value={state.title}
                    onChangeText={title => this.setState({ title })}
                    placeholder='Title'
                />
                <TextInput
                    style={styles.inputBox}
                    value={state.description}
                    onChangeText={description => this.setState({ description })}
                    placeholder='Description'
                />
                <CheckBox
                    center
                    title='Click Here'
                    checked={this.state.checked}
                    onPress={() => this.onPress()}
                />
                <View>
                    <View>
                        <Button onPress={showDatepicker} title="Show date picker!" />
                    </View>
                    <View>
                        <Button onPress={showTimepicker} title="Show time picker!" />
                    </View>
                        {show && (
                            <DateTimePicker
                            testID="dateTimePicker"
                            value={date}
                            mode={mode}
                            is24Hour={true}
                            display="default"
                            onChange={onChange}
                            />
                        )}
                    </View>
                <TouchableOpacity style={styles.button} onPress={handleEventCreation}>
                    <Text style={styles.buttonText}>Create Event</Text>
                </TouchableOpacity>
            </View>
        )
    }

You are using this.setState inside a functional component.您在功能组件中使用this.setState You cannot mix those two together (you can only use it inside a class component).您不能将这两者混合在一起(您只能在 class 组件中使用它)。 If you want to use checked you need to add const [checked, setChecked] = useState(false);如果要使用checked ,则需要添加const [checked, setChecked] = useState(false); and use setChecked to set state of checked.并使用setChecked设置检查的 state。 Same goes for title and description . titledescription也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM