Javascript TypeError:undefined不是React Native中的对象(计算';addTodo(标题、说明)。然后';)
我有个例外 TypeError:undefined不是对象(计算'addTodo(title,description.)。然后') 函数工作正常,状态更新正常,但未按预期工作 AddTodo.js:Javascript TypeError:undefined不是React Native中的对象(计算';addTodo(标题、说明)。然后';),javascript,reactjs,react-native,react-native-navigation,context-api,Javascript,Reactjs,React Native,React Native Navigation,Context Api,我有个例外 TypeError:undefined不是对象(计算'addTodo(title,description.)。然后') 函数工作正常,状态更新正常,但未按预期工作 AddTodo.js: import { useNavigation } from '@react-navigation/native'; import React , {useContext, useState} from 'react'; import { View } from 'react-native'; imp
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 ;
附言:-在我添加then和catch方法之前,handleonSubmit运行良好。现在我收到了错误,但我的状态仍在添加中。如果您想更改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()
})
}
addTodo
不返回Promise
这就是为什么您没有。然后在handleOnSubmit()
@norbitrial中()
如何更改addTodo
以返回承诺。
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
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()
})
}