Skip to content

My program is ‘skipping’ a fetch request, React.js

Its really weird.

The first fetch I do works appropriately, but when doing the second fetch in my handleSubmit() method it sort of ‘skips’ it. It goes on, never enters the .then statements, doesn’t print an error. Ive tried with other APIs, but honestly it should work fine since the first take is almost identical and it worked. Ive tried rewriting with a return statement as well…

export default function FormContainer() {
    const [api, setApi] = useState()
    const [showText, setShowText] = useState(false)
    const [apiUrl, setApiUrl] = useState('')
    const [text, setText] = useState('')
    const [display, setDisplay] = useState('')
    const [page, setPage] = useState('')
    useEffect(() => {
        fetch('') //FIRST TRY, works
        .then(response => response.json())
        .then(response => setApi(response))
    function handleRadio(event){
    function handleText(event){
    function handleSubmit(event){
        let list = {};
        let found = false;
        console.log(apiUrl); //Prints
        fetch(apiUrl) //SECOND TRY, fails
        .then(response =>{
            console.log(response); //Never prints
            return response.json();
        .then(response => {
        .catch(error => {
            console.error(error); //Doesnt run
   => {
                if( === text.toUpperCase()){
                    found = true
                    let toDisplay = ''
                    if(page === 'people'){
                    }else if(page === 'planets'){
                    }else if(page === 'films'){
                    }else if(page === 'species'){
                    }else if(page === 'vehicles'){
                if( !== null){
                    fetch( //DIDNT GET TO TRY THIS
                    .then(response => response.json())
                    .then(response => {list = response})
                    found = true
                    setDisplay('Object not found, are you sure you typed it in correctly?')
  return (
    <div >
      <FormRadios handleRadio={handleRadio}/>
      {showText ? <FormComponent text={text} handleText={handleText} handleSubmit={handleSubmit}/> : null}
      <FormOutput display={display}/>

Of course, I welcome any kind of advice on my code, since Im brand new on React.js and using hooks. Thanks in advance!


It appears successful, but just now I realized what my problem was. When fetching again for the api, i jump to the next line of code immediately, but the fetch isnt as instant as the code running. I had an exception going on after the fetch (because im trying to use info from the fetch), so the fetch couldnt finish fast enough to use the information correctly, then the exception runs and, of course, the console logs dont work. Thats basically what happened, and quite interesting as well. But thanks for all the help, it really helped me fin what was going on.