首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从钩子发出/触发事件

如何从钩子发出/触发事件
EN

Stack Overflow用户
提问于 2022-03-17 14:48:21
回答 1查看 35关注 0票数 0

对于如何从一个useEffect看起来相当错误的钩子中触发一些实际事件(并且无论如何都会导致不当行为),我的反应和困难都是很新的。

有什么最好的做法吗?或者说使用钩子是个坏主意?

这是一个非常粗糙的文件选择器示例。主要要求是,如果文件无效,processFile应该触发一次警报。

代码语言:javascript
复制
import { ChangeEvent, useEffect, useState } from 'react';

export const useUploader = () => {
  const [file, setFile] = useState<File | undefined>();
  const [isInvalid, setIsInvalid] = useState(false);

  const processFile = (inputfile: File) => {
    const fileTooBig = inputfile.size > 1 * 1024 * 1024;

    setFile(fileTooBig ? undefined : inputfile);

    setIsInvalid(fileTooBig);
    if (fileTooBig) {
      // trigger event?
    }
  };

  return { file, isInvalid, processFile };
};
代码语言:javascript
复制
export const ImageUpload = () => {
  const { file, isInvalid, processFile } = useUploader();
  const { t } = useTranslation();

  // "Wrong appraoch" as it would also show the alert when
  // translation changes or a hot-reload happens during development
  useEffect(() => {
    if (isInvalid) {
      alert(t('Invalid file selected'));
    }
  }, [isInvalid, t]);

  const onChange = (e: ChangeEvent<HTMLInputElement>) => {
    if (e.currentTarget.files) {
      processFile(e.currentTarget.files[0]);
    }
  };

  return (
    <>
      <input type="file" onChange={onChange}></input>
      <span>Selected file: {file?.name}</span>
      <span>IsInvalid: {isInvalid ? 'Invalid' : 'All fine'}</span>
    </>
  );
};
EN

回答 1

Stack Overflow用户

发布于 2022-03-17 14:58:39

您只需使用一个状态来表示无效状态。

代码语言:javascript
复制
import { ChangeEvent, useEffect, useState } from 'react';

export const useUploader = () => {
  /**
   * one could use three states here
   * 
   * undefined means the empty state
   * null      means an error state
   * file      means success state
   */
  const [file, setFile] = useState<File | undefined>();

  const processFile = (inputfile: File) => {
    const fileTooBig = inputfile.size > 1 * 1024 * 1024;
    setFile(fileTooBig ? null : inputfile);
  };

  const isInvalid = file === null;

  return { file, isInvalid, processFile };
};

export const ImageUpload = () => {
  const { file, isInvalid, processFile } = useUploader();

  /**
   * this is just, okay.
   */
  useEffect(() => {
    if (isInvalid) {
      alert('Invalid file selected');
    }
  }, [isInvalid]);

  const onChange = (e: ChangeEvent<HTMLInputElement>) => {
    if (e.currentTarget.files) {
      processFile(e.currentTarget.files[0]);
    }
  };

  return (
    <>
      <input type="file" onChange={onChange}></input>
      <span>Selected file: {file?.name}</span>
      <span>IsInvalid: {isInvalid ? 'Invalid' : 'All fine'}</span>
    </>
  );
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71514350

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档