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.
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')
How do I manage this , could any one help? Thank you!!