简体   繁体   English

在 React 中接受 setState 作为 function 参数是不好的做法吗?

[英]Is it bad practice to accept setState as a function parameter in React?

Basically, before uploading an image to the firebase, I'm trying to control the input as:基本上,在将图像上传到 firebase 之前,我试图将输入控制为:

export const controlThumbnail = (selectedThumbnail, setThumbnailError) => {
  if (!selectedThumbnail) {
    setThumbnailError('Please select a thumbnail!');

    return;
  }

  if (!selectedThumbnail.type.includes('image')) {
    setThumbnailError('Please select an image!');

    return;
  }

  if (selectedThumbnail.size > 1000000) {
    setThumbnailError('Image size must be less than 1MB!');

    return;
  }

  setThumbnailError(null);
};

which I call the above method from /lib/controlThumbnail.js to:我将上述方法从 /lib/controlThumbnail.js 调用为:

import { controlThumbnail } from '../../lib/controlThumbnail';
    
const Signup = () => {
  const [userInfo, setUserInfo] = useState({
    name: '',
    email: '',
    password: '',
    thumbnail: null
  });
  const [thumbnailError, setThumbnailError] = useState(null);


  const userInputHandler = (e) => {
    setUserInfo((prevUserInfo) => {
      if (e.target.name === 'thumbnail') {
        const thumbnail = e.target.files[0];
        controlThumbnail(thumbnail, setThumbnailError);

        return { ...prevUserInfo, thumbnail };
      } else {
        return { ...prevUserInfo, [e.target.name]: e.target.value };
      }
    });
  };
...

so, this is now works correctly, but I wonder if this is the good way of doing it?所以,这现在可以正常工作,但我想知道这是否是这样做的好方法? Or should I put the control method inside the component and never give setState as parameter?或者我应该将控制方法放在组件内,并且永远不要将 setState 作为参数?

It is subjective.这是主观的。 Personally, I think the controlThumbnail function is not the right place to make that abstraction.就个人而言,我认为controlThumbnail function 不是进行该抽象的正确位置。 In here, you are really only providing validation.在这里,您实际上只是提供验证。 You don't need to give it the responsibility to validate AND set some state.您不需要赋予它验证和设置一些 state 的责任。

You could create a pure validation function, and just use the return of this to update the state in your Signup component:您可以创建一个纯验证 function,然后使用它的返回来更新您的Signup组件中的 state:

const validateThumbnail = (thumbnail) => {
  if (!thumbnail) {
    return 'Please select a thumbnail!';
  }

  if (!thumbnail.type.includes('image')) {
    return 'Please select an image!'
  }

  if (thumbnail.size > 1000000) {
    return 'Image size must be less than 1MB!'
  }

  return null
}

const Signup = () => {
  const [userInfo, setUserInfo] = useState({
    name: '',
    email: '',
    password: '',
    thumbnail: null
  });
  const [thumbnailError, setThumbnailError] = useState(null);


  const userInputHandler = (e) => {
    setUserInfo((prevUserInfo) => {
      if (e.target.name === 'thumbnail') {
        const thumbnail = e.target.files[0];
        setThumbnailError(validateThumbnail(thumbnail));
        return { ...prevUserInfo, thumbnail };
      }
      return { ...prevUserInfo, [e.target.name]: e.target.value };
    });
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM