0

The first function postOpenai was called in useEffect() to get data and setState setWriting successfully , then I try to call function splitFunc after. I use callback in setWriting() like following:

     const [writing, setWriting] = useState(null);
     const [isPending, setIsPending] = useState(true);
     const [error, setError] = useState(null);

     const postOpenai = async function () {
          console.log(params);
          await Parse.Cloud.run("postOpenai", params)
               .then(resp => {
                    setWriting(resp[0].text, () => {
                         splitFunc()
                    });
                    setIsPending(false);
                    setError(null);
               })
               .catch(error => {
                    setError("Failed to generate", error);
                    setIsPending(false);
               });
     };

     let writingSets = [];

     const splitFunc = () => {
          console.log(typeof (writing))
          writingSets = writing.split("\n");
          console.log(writingSets);
     };

     useEffect(() => {
          console.log("1", writing);
          console.log(Boolean(writing == null));
          if (writing == null) {
               postOpenai();
          } else {
               alert("alert! can not repeatedly generate ")
          }
          // eslint-disable-next-line react-hooks/exhaustive-deps 
     }, []);

I get an error: state updates from the usestate() and usereducer() hooks don't support the second callback argument.

enter image description here

So I try using useEffect() to run the second function splitFunc() like this:

useEffect(() => {
          console.log("2" + writing);
          splitFunc()
     }, [writing]);

It failed again, gets errors showing setWriting() hasn't been succeed: Cannot read properties of null (reading 'split') enter image description here

How do I manage this , could any one help? Thank you!!

  • 1
    `setState` doesn't take second argument, it's not a class component – Konrad Nov 22 '22 at 15:41
  • Does this answer your question? [The useState set method is not reflecting a change immediately](https://stackoverflow.com/questions/54069253/the-usestate-set-method-is-not-reflecting-a-change-immediately) – Konrad Nov 22 '22 at 15:41
  • It doesn't work using useEffect() as well... do I miss something? – FormosaMephitis Nov 22 '22 at 16:49

0 Answers0