簡體   English   中英

TypeError:undefined 不是 React Native 中的對象(正在評估“addTodo(title, description).then”)

[英]TypeError: undefined is not an object (evaluating 'addTodo(title, description).then') in React Native

我收到 Exception TypeError: undefined is not an object (evaluating 'addTodo(title, description).then') 該函數工作正常,狀態更新正常,但未按預期工作。

添加Todo.js :

import { useNavigation } from '@react-navigation/native';
import React , {useContext, useState} from 'react';
import { View } from 'react-native';
import { Input , Button} from 'react-native-elements';
import { TextInput } from 'react-native-gesture-handler';
import {cardContext} from '../contextApi/cardContext';


const NewTodoInput = () =>{
const { addTodo } = useContext(cardContext);
const navigation = useNavigation();


const [title , setTitle] = useState()
const [description , setDescription] = useState()

const handleOnSubmit = ()=>{
    
    addTodo(title,description)
    .then((data)=>{
        navigation.navigate('TodoApp');
        console.log('Added Succesfully : ' + data);
    })
    .catch((e)=>{console.log(e)});

}
return (
    <View>
    <Input 
    placeholder = 'Title'
    onChangeText= {(val)=>{setTitle(val)}}
    />
    <Input 
    placeholder = 'Your todo'
    multiline = {true}
    onChangeText = {(val) => {setDescription(val)}}
    />
    <Button 
        buttonStyle= {{backgroundColor : 'black'}}
        title = 'Submit'
        onPress = {handleOnSubmit}
        
        
    />
    </View>

)

}

export default NewTodoInput ;

上下文.js

import React , {createContext, useState} from 'react'

export const cardContext = createContext();

const Data = (props) =>{
    const [data , setData] = useState(
        //todo Add id to the data
        [
            {title : 'This is title 1' , Description : 'this is description 1'},
            {title : 'This is title 2' , Description : 'this is description 2'},
            {title : 'This is title 3' , Description : 'this is description 3'},
            {title : 'This is title 4' , Description : 'this is description 4'},



        ]
        
    ) 
    // Addd id to the data
    const addTodo = (title , description) =>{
            
            setData( [...data , {title : title, Description : description}] )
    }
    const removeTodo = (id) => {
        setData(data.filter(data => data.id != id))

    }
    return (
        <cardContext.Provider value= {{data , addTodo}} >
        {props.children}
        </cardContext.Provider>

    )
}
export default Data;

我收到以下錯誤:

TypeError: undefined is not an object (evaluating 'addTodo(title, description).then')
- node_modules\react-native\Libraries\LogBox\LogBox.js:148:8 in registerError
- node_modules\react-native\Libraries\LogBox\LogBox.js:59:8 in errorImpl
- node_modules\react-native\Libraries\LogBox\LogBox.js:33:4 in console.error
- node_modules\expo\build\environment\react-native-logs.fx.js:27:4 in error
- node_modules\react-native\Libraries\Core\ExceptionsManager.js:104:6 in reportException
- node_modules\react-native\Libraries\Core\ExceptionsManager.js:171:19 in handleException
- node_modules\react-native\Libraries\Core\setUpErrorHandling.js:24:6 in handleError
- node_modules\expo-error-recovery\build\ErrorRecovery.fx.js:9:32 in ErrorUtils.setGlobalHandler$argument_0
- node_modules\regenerator-runtime\runtime.js:63:36 in tryCatch
- node_modules\regenerator-runtime\runtime.js:293:29 in invoke
- node_modules\regenerator-runtime\runtime.js:63:36 in tryCatch
- node_modules\regenerator-runtime\runtime.js:154:27 in invoke
- node_modules\regenerator-runtime\runtime.js:164:18 in PromiseImpl.resolve.then$argument_0
- node_modules\react-native\node_modules\promise\setimmediate\core.js:37:13 in tryCallOne
- node_modules\react-native\node_modules\promise\setimmediate\core.js:123:24 in setImmediate$argument_0
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:130:14 in _callTimer
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:181:14 in _callImmediatesPass
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:441:30 in callImmediates
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:387:6 in __callImmediates
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:135:6 in __guard$argument_0
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:134:4 in flushedQueue
* [native code]:null in flushedQueue
* [native code]:null in invokeCallbackAndReturnFlushedQueue

TypeError: undefined is not an object (evaluating 'addTodo(title, description).then')
- node_modules\react-native\Libraries\LogBox\LogBox.js:148:8 in registerError
- node_modules\react-native\Libraries\LogBox\LogBox.js:59:8 in errorImpl
- node_modules\react-native\Libraries\LogBox\LogBox.js:33:4 in console.error
- node_modules\expo\build\environment\react-native-logs.fx.js:27:4 in error
- node_modules\react-native\Libraries\Core\ExceptionsManager.js:104:6 in reportException
- node_modules\react-native\Libraries\Core\ExceptionsManager.js:171:19 in handleException
- node_modules\react-native\Libraries\Core\setUpErrorHandling.js:24:6 in handleError
- node_modules\expo-error-recovery\build\ErrorRecovery.fx.js:9:32 in ErrorUtils.setGlobalHandler$argument_0
- node_modules\regenerator-runtime\runtime.js:63:36 in tryCatch
- node_modules\regenerator-runtime\runtime.js:293:29 in invoke
- node_modules\regenerator-runtime\runtime.js:63:36 in tryCatch
- node_modules\regenerator-runtime\runtime.js:154:27 in invoke
- node_modules\regenerator-runtime\runtime.js:164:18 in PromiseImpl.resolve.then$argument_0
- node_modules\react-native\node_modules\promise\setimmediate\core.js:37:13 in tryCallOne
- node_modules\react-native\node_modules\promise\setimmediate\core.js:123:24 in setImmediate$argument_0
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:130:14 in _callTimer
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:181:14 in _callImmediatesPass
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:441:30 in callImmediates
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:387:6 in __callImmediates
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:135:6 in __guard$argument_0
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:134:4 in flushedQueue
* [native code]:null in flushedQueue
* [native code]:null in invokeCallbackAndReturnFlushedQueue

PS :- handleonSubmit 一直工作正常,直到我添加了它並為其添加了 catch 方法。 現在我收到錯誤,但我的狀態仍在添加。

如果您想更改 addTodo 使其返回評論中所述的承諾,那么這里是:

const addTodo = (title , description) =>{
   setData( [...data , {title : title, Description : description}] )
   return new Promise((resolve, reject) => {
       resolve("data") // Do something here preferable what you need inside .then()
                      })
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM