【问题标题】:React Native - Property 'then' does not exist on type '(dispatch: any) => Promise<void>'.ts(2339)React Native - 类型 '(dispatch: any) => Promise<void>'.ts(2339) 上不存在属性 'then'
【发布时间】:2021-10-29 10:55:22
【问题描述】:

在我的Register.tsx 文件中,我有以下代码:

 <View style={styles.keyboardView}>
        <Formik
          initialValues={{
            firstname: '',
            lastname: '',
            email: '',
            password: '',
          }}
          onSubmit={values => {
            dispatch(authAction.registerUser(values))
              .then(() => {
                navigateTo('HomeScreen');
              })
              .catch(err => console.log(err));
          }}
          validationSchema={RegisterValidationSchema}>

Visual Studio 代码突出显示 .then(),错误为:Property 'then' does not exist on type '(dispatch: any) =&gt; Promise&lt;void&gt;'.ts(2339)

此外,err 也在 Visual Studio 代码中显示此错误:Parameter 'err' implicitly has an 'any' type.ts(7006)

这是我的store.js 代码的样子:

import {createStore, applyMiddleware, combineReducers} from 'redux';
import thunk from 'redux-thunk';
import {composeWithDevTools} from 'redux-devtools-extension';

import authReducer from './reducers/authReducer';

const rootReducer = combineReducers({
  auth: authReducer,
});

const middleware = composeWithDevTools(applyMiddleware(thunk));

const store = createStore(rootReducer, middleware);

export default store;

请注意,我使用的是.js 文件而不是.tsx 文件。所以我混合了两个文件。这会导致问题吗?

我该如何解决这个问题?

【问题讨论】:

    标签: reactjs react-native redux react-redux


    【解决方案1】:

    该错误是 TypeScript 特定的错误。

    我猜,您使用的是 redux 的 dispatch(如果我的猜测有误,请告诉我)。导入时尝试添加:

    const dispatch:any = useDispatch()
    

    另外,在您的catch 中,尝试:

    .catch((err:any) => console.log(err));
    

    注意:你可以在any 旁边添加任何你需要的类型,但我相信any 也可以使用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-23
      • 2021-11-04
      • 2020-10-08
      • 1970-01-01
      • 2021-12-28
      • 2021-01-10
      • 2019-08-11
      相关资源
      最近更新 更多