Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

reactjs - I'm trying to use useEffect' for changing window. I do not get any error, but it doesn't work. What is wrong? How to make it work?

Through dropzone, I'm uploading or dropping files. What I want to do is to open a new window type, based on the file type that has been dropped. To accomplish this, I'm trying to use the useEffect hook. But it doesn't produce any error and doesn't work:

const [win, setWin] = useState("A-Type");

let accepted_file = acceptedFiles[0];

useEffect(() => {
  function handleSecondWindowType () {
    if (accepted_file.type.includes('image/')) {
      setWin("A-Type")
    }
    else {
      setWin("B-Type");
    }
  }
},[accepted_file]);
question from:https://stackoverflow.com/questions/65651808/im-trying-to-use-useeffect-for-changing-window-i-do-not-get-any-error-but-it

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Looks like you have created an internal function but forgot to call it. You have to call it or nothing happened:

useEffect(() => {
  // call it
  handleSecondWindowType();

  function handleSecondWindowType () {
    if (accepted_file.type.includes('image/')) {
      setWin("A-Type")
    }
    else {
      setWin("B-Type");
    }
  }
},[accepted_file]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...