React async/await 用法 - 未处理的拒绝(TypeError):无法读取未定义的属性(读取“then”)

发布于 2025-01-11 01:07:55 字数 998 浏览 0 评论 0原文

我正在使用化简器将数据添加到 addItem 数组。

我想等到项目实际添加后再继续。我已经实现了它,如下所示,

const [addItems, dispatch] = useReducer((state, action) => {
  switch (action.type) {
    case 'add':
      return [
        ...state,
        {
          id: state.length,
          data: action.data
        }
      ];
    default:
      return state;
  }
}, []);

const linkDataHandler = async(samplesData, test, e) => {
    
    const isSelected =  e.target.checked;

    await callDispatch(samplesData.id, test.testList.id, isSelected) 
    .then( r =>{
    //do something
    }});
  
    };

const callDispatch = (sampleId, testId, checked) => {
      
      const linkedData = {
        sampleId: sampleId,
        TestId: testId,
        isSelected: checked
      };
  
      dispatch({
        type: "add",
        data: linkedData
      });
    };

在复选框 onChange() 事件上调用函数 linkDataHandler 。

它正在给我

未处理的拒绝(TypeError):无法读取未定义的属性(读取“then”)

I am adding data to an addItem array using reducers.

I want to wait until the item is actually added before going forward..I have implemented it as follows

const [addItems, dispatch] = useReducer((state, action) => {
  switch (action.type) {
    case 'add':
      return [
        ...state,
        {
          id: state.length,
          data: action.data
        }
      ];
    default:
      return state;
  }
}, []);

const linkDataHandler = async(samplesData, test, e) => {
    
    const isSelected =  e.target.checked;

    await callDispatch(samplesData.id, test.testList.id, isSelected) 
    .then( r =>{
    //do something
    }});
  
    };

const callDispatch = (sampleId, testId, checked) => {
      
      const linkedData = {
        sampleId: sampleId,
        TestId: testId,
        isSelected: checked
      };
  
      dispatch({
        type: "add",
        data: linkedData
      });
    };

function linkDataHandler is called on a checkbox onChange() event.

It is giving me

Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'then')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瀞厅☆埖开 2025-01-18 01:07:55

我已经重写了 callDispatch 如下。因为预计会返回一个承诺

const callDispatch = (sampleId, testId, checked) => {
               

      const linkedData = {
        sampleId: sampleId,
        TestId: testId,
        isSelected: checked
      };
  
      return Promise.resolve(
      dispatch({
        type: "add",
        data: linkedData
      }));
    };

I have re-written callDispatch as follows. Since it is expected to return a promise

const callDispatch = (sampleId, testId, checked) => {
               

      const linkedData = {
        sampleId: sampleId,
        TestId: testId,
        isSelected: checked
      };
  
      return Promise.resolve(
      dispatch({
        type: "add",
        data: linkedData
      }));
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文