Don't return api when input reverts to empty

Viewed 36

I am calling an api that return stock info once a user types in a certain symbol. My problem is that once the input clears the original api is called and it returns null because the api returns nothing. If I enter in another symbol it comes up but once the fetch from the cleared input is fetch it returns zero and cancels out the api that called the symbol. Is there some way I can stop this from happening?

  componentDidUpdate(prevProps, prevState, snapShot) {
        const { userInput, symbol1 } = this.state;

        if (prevState.userInput !== userInput) {
            fetch(`https://finnhub.io/api/v1/quote?symbol=${userInput}&token=budo2rv48v6spq9og4p0`)
                .then(res => res.json())
                .then(responses => this.setState({
                    symbol1: responses
                }))
        }
        else if (userInput === '' || prevState.userInput === '') {
            return userInput
        }

    }
0 Answers
Related