Home > OS >  Asynchronous function makes request without data when page reloads
Asynchronous function makes request without data when page reloads

Time:01-27

So the problem here is I have this asynchronous function that makes request with string variable, but when page reloads it makes same request without this string despite the fact that variable itself is not empty. As a result I am receiving an error 'Bad Request' because text was not provided. Would someone be so kindly to explain me how thing works here so i could fix it so that those requests after page reloading were sent with data ?!

    const { text, setText } = useContext(TextContext);
    const [isLoading, setLoading] = useState(true);
    const [entitiesData, setEntitiesData] = useState([]);
    
    const call_razor = async (text_encoded) => {

        try {
            console.log(text_encoded) //here it shows data even when described error occurs after
            const response = await axios.post('https://api.textrazor.com/',
            "extractors=entities&text=" text_encoded,
            {
                headers: {
                    'x-textrazor-key': API_KEY,
                    'Content-Type': 'application/x-www-form-urlencoded',
                }
            });

            setEntitiesData(response.data.response.entities)
            setLoading(false)
        } catch (err) {
            console.log(err)
            console.log(err.response.data.error)
            console.log(err.response)
            setLoading(false)
        }
    }

    const dataFetch = async () => {
        let textEncoded = encodeURIComponent(text)
        await call_razor(textEncoded).then(() => splitIntoSentences())
    }

    useEffect(() => {
        if (text) {
            localStorage.setItem('text', text)
        } else {
            setText(localStorage.getItem('text'))
        }
        dataFetch();
    }, [isLoading]);

CodePudding user response:

The problem you're encountering is likely due to the fact that the useEffect hook is running before the text value is set from the TextContext context object.

One way to fix this issue is to move the useEffect hook to the parent component that is providing the TextContext context object, and pass the text value as a prop to the child component that is making the API request. This way, the text value will be available to the child component before the useEffect hook is run.

Another way would be to add a check for the text variable being empty or not in dataFetch() function, If it's empty, you can set isLoading to false so that it doesn't trigger the useEffect callback function.

    const dataFetch = async () => {
    if(text){
        let textEncoded = encodeURIComponent(text)
        await call_razor(textEncoded).then(() => splitIntoSentences())
    }else {
        setLoading(false)
    }
}

You can also move the dataFetch() function call inside the useEffect callback after the text value is set.

useEffect(() => {
    if (text) {
        localStorage.setItem('text', text)
        dataFetch();
    } else {
        setText(localStorage.getItem('text'))
    }
}, [isLoading]);
  • Related