【问题标题】:How to supply object to thunk in typescript如何在打字稿中为 thunk 提供对象
【发布时间】:2020-06-20 15:18:57
【问题描述】:

这里有一个带有单个属性的示例 - “消息” - 提供给 thunk:

https://redux.js.org/recipes/usage-with-typescript

// src/thunks.ts

import { Action } from 'redux'
import { sendMessage } from './store/chat/actions'
import { RootState } from './store'
import { ThunkAction } from 'redux-thunk'

export const thunkSendMessage = (
  message: string
): ThunkAction<void, RootState, unknown, Action<string>> => async dispatch => {
  const asyncResp = await exampleAPI()
  dispatch(
    sendMessage({
      message,
      user: asyncResp,
      timestamp: new Date().getTime()
    })
  )
}

function exampleAPI() {
  return Promise.resolve('Async Chat Bot')
}

如何编写相同的示例但提供一个对象 - “{message}”

// src/thunks.ts

import { Action } from 'redux'
import { sendMessage } from './store/chat/actions'
import { RootState } from './store'
import { ThunkAction } from 'redux-thunk'

export const thunkSendMessage = ({
  message: string
}): ThunkAction<void, RootState, unknown, Action<string>> => async dispatch => {
  const asyncResp = await exampleAPI()
  dispatch(
    sendMessage({
      message,
      user: asyncResp,
      timestamp: new Date().getTime()
    })
  )
}

function exampleAPI() {
  return Promise.resolve('Async Chat Bot')
}

【问题讨论】:

    标签: reactjs typescript thunk


    【解决方案1】:

    在 ES6 的情况下,您只需从消息对象参数中删除数据类型(字符串)。

    // src/thunks.ts
    
    import { Action } from 'redux'
    import { sendMessage } from './store/chat/actions'
    import { RootState } from './store'
    import { ThunkAction } from 'redux-thunk'
    
    interface Message {
    messageText : string,
    //...other properties if required ...
    }
    
    export const thunkSendMessage = (
      message : Message
    ): ThunkAction<void, RootState, unknown, Action<string>> => async dispatch => {
      const asyncResp = await exampleAPI()
      dispatch(
        sendMessage({
          message: message.messageText,
          user: asyncResp,
          timestamp: new Date().getTime()
        })
      )
    }
    
    function exampleAPI() {
      return Promise.resolve('Async Chat Bot')
    }
    

    【讨论】:

    • 我正在寻找一个符合打字稿条件的答案。
    • 如何将该形状导出到函数组件:例如:const myComponent = ({thunkSendMessage:whatTypeHere}) => ()
    • 感谢您的回答。我已经在上面发布了回复以更好地理解问题 -
    【解决方案2】:

    好的,我已经创建了一个示例,以便更容易理解问题。

    actions.tsx - 这似乎通过了 - (感谢 MuhhammadUmarFarooq - 你对我的问题的回答。)

    import {RootState} from 'src/index';
    import { Action } from 'redux'
    import { ThunkAction } from 'redux-thunk';
    import { actionTypes as at } from './actionTypes';
    
    type shapePayload = {
      message: string
    };
    
    const acExample = (
      payload: shapePayload
    ): ThunkAction<void, RootState, unknown, Action<string>> => dispatch => {
      const {message} = payload;
      return dispatch({
        type: at.AC_TYPE_EXAMPLE,
        payload: {
          message,
          somethingelse: true
        }
      });
    }
    
    export type shapeExample = ReturnType<typeof acExample>;
    
    export {
      acExample
    };
    

    组件使用动作创建器 - 这是这里的问题:

    import React, {FC} from 'react';
    import { connect } from 'react-redux';
    import {shapeExample, acExample} from 'src/store/examples/actions';
    
    type shape = {
      acExample: shapeExample
    }
    const myComponent:FC<shape> = ({acExample}) => (
      <button onClick={() => {
        acExample({message: 'cheese'});
      }} >
        trigger store update
      </button>
    );
    
    export default connect(
      () => ({}),
      {
        acExample
      }
    )(myComponent);
    
    • 错误 - acExample: 预期 3 个参数

    • 错误 - (myComponent):

    “FC”类型的 TypeScript 参数不可分配给“ComponentType void; },形状>>'。类型 'FunctionComponent' 不可分配给类型 'FunctionComponent void; },形状>>'。属性“propTypes”的类型不兼容。键入'弱验证映射 | undefined' 不可分配给类型 'WeakValidationMap void; }, 形状>> |不明确的'。类型 'WeakValidationMap' 不可分配给类型 'WeakValidationMap void; },形状>>'。属性“acExample”的类型不兼容。类型 'Validator> | undefined' 不可分配给类型 'Validator void> |不明确的'。类型 'Validator>' 不可分配给类型 'Validator void>'。类型 'ThunkAction' 不可分配给类型 '(payload: shapePayload) => void

    【讨论】:

    • 请在问题中添加问题详细信息,并带有一些标题,例如更新...以便其他人也可以回答
    猜你喜欢
    • 2018-04-29
    • 2021-12-17
    • 1970-01-01
    • 2018-09-29
    • 2023-03-07
    • 2020-10-05
    • 2021-06-29
    • 2021-11-02
    • 1970-01-01
    相关资源
    最近更新 更多