【问题标题】:TypeError: evt.target is undefined类型错误:evt.target 未定义
【发布时间】:2021-10-24 23:19:26
【问题描述】:

我已尝试实现自动完成输入字段。相应的文档可以在here 找到。如果需要更多代码,请告诉我。但是,涉及的代码有点长,我不想把它全部放在这里。

错误:

如果上面显示 2 个错误中的 1 个,请不要感到惊讶,但无论出于何种原因,相同的错误消息会显示两次。

我的代码:

AlarmstichworteAutocompleteForm/index.tsx

import * as React from 'react';
import { memo } from 'react';
import { useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { useSelector, useDispatch } from 'react-redux';
import { AutoComplete } from 'primereact/autocomplete';
import { messages } from './messages';
import {
  selectSuggestionInput,
  selectData,
  selectLoading,
  selectError,
} from './slice/selectors';
import {getAlarmstichworte} from './slice/saga';
import { AlarmstichwortErrorType } from './slice/types';
import { useAlarmstichworteAutocompleteFormSlice } from './slice';
import { fetchAlarmstichworte, fetchAlarmstichworteByName } from './slice/api';
import { Alarmstichwort } from '../../../../../../../types/Alarmstichwort';
import { lazyLoad } from '../../../../../../../utils/loadable';

interface Props {}

export const AlarmstichworteAutocompleteForm= memo((props: Props) => {

  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  const {t,  i18n } = useTranslation();
  const {actions} = useAlarmstichworteAutocompleteFormSlice();
  const dispatch = useDispatch();

  const suggestionInput = useSelector(selectSuggestionInput);
  const data = useSelector(selectData);
  const loading = useSelector(selectLoading);
  const error = useSelector(selectError);

  const useEffectOnMount = (effect: React.EffectCallback) => {
    // eslint-disable-next-line react-hooks/exhaustive-deps
    useEffect(effect, []);
  };

  const onChangeSuggestionInput = (evt: React.ChangeEvent<HTMLInputElement>) => {
    dispatch(actions.changeSuggestionInput(evt.target.value)); // <-- Part of the error
    dispatch(actions.loadAlarmstichworte());
  };


  useEffectOnMount(() => {
    dispatch(actions.loadAlarmstichworte());
  });

  return(
    <div>
      <AutoComplete 
      dropdown 
      value={suggestionInput}
      suggestions={data} 
      onChange={(evt) => onChangeSuggestionInput(evt.value)} // <-- Part of the error
      />
    </div>
   
    );
});

其背后的想法如下:当输入输入到输入字段时,动作changeSuggestionInput将suggestInput添加到状态。再次运行actions.loadAlarmstichworte时,只显示API搜索输入与suggestInput匹配的结果。

更新:

如果我将 onChange={(evt) =&gt; onChangeSuggestionInput(evt.value)} 替换为 onChange={onChangeSuggestionInput},则会收到以下错误:

`No overload matches this call.
  Overload 1 of 2, '(props: AutoCompleteProps | Readonly<AutoCompleteProps>): AutoComplete', gave the following error.
    Type '(evt: React.ChangeEvent<HTMLInputElement>) => void' is not assignable to type '(e: AutoCompleteChangeParams) => void'.
      Types of parameters 'evt' and 'e' are incompatible.
        Type 'AutoCompleteChangeParams' is missing the following properties from type 'ChangeEvent<HTMLInputElement>': nativeEvent, currentTarget, bubbles, cancelable, and 8 more.   
  Overload 2 of 2, '(props: AutoCompleteProps, context: any): AutoComplete', gave the following error.
    Type '(evt: React.ChangeEvent<HTMLInputElement>) => void' is not assignable to type '(e: AutoCompleteChangeParams) => void'.  TS2769```

【问题讨论】:

  • 您不想将evt.value 发送到onChangeSuggestionInput,而是发送整个事件元素,然后在onChangeSuggestionInput 中,您可以创建变量以从evt 中提取值、目标或其他内容你需要使用

标签: javascript reactjs typescript react-redux redux-saga


【解决方案1】:

错误就在这里。

 <AutoComplete 
      dropdown 
      value={suggestionInput}
      suggestions={data} 
      onChange={(evt) => onChangeSuggestionInput(evt.value)} 
      />

这里evt.value作为参数传递。

 const onChangeSuggestionInput = (evt:React.ChangeEvent<HTMLInputElement>)

在这个函数中,你正在访问evt.target

您可以在调用部分删除错误。

    onChange={onChangeSuggestionInput} 

【讨论】:

  • 如果我这样做,我会收到我在帖子中添加的错误。
  • 相当于onChange={(evt) =&gt; onChangeSuggestionInput(evt)}。如果这不起作用,那么您可以检查AutoComplete是否需要使用onChange或其他方法来检测输入变化。
  • 我仍然找不到确切的错误,因为应该指定 documentation onChange 但我会继续寻找。感谢您的帮助!
猜你喜欢
  • 2019-10-31
  • 2016-08-07
  • 2019-01-07
  • 2020-01-28
  • 2014-10-17
  • 2022-01-10
  • 2019-12-24
  • 2020-02-27
  • 2012-09-12
相关资源
最近更新 更多