1

I have an async function in a file (someFile.js) in my React app and I want the returned string from that function (someFunction) to be inserted in the webpage when it's finished. The problem is with the code below, I think the webpage loads before the promise is resolved, so the webpage appears blank. Can someone help me and see if there's a problem with my code below? The code below works when someFunction isn't an async function.

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App.js';
import reportWebVitals from './reportWebVitals.js';

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

reportWebVitals();

App.js

import Header from "./components/Header.js";

function App() {
    return (
        <div className="container">
            <Header />
        </div>
    );
}

export default App;

Header.js

import someFunction from "./someFile.js";

const Header = () => {
    let text = someFunction();
    return (<h1>{text}</h1>)
}

export default Header;

someFile.js

const someFunction = async () => {
    let x = await someAsyncFunc();
    return x; //x is a string
}

export default someFunction;
1

2 Answers 2

0

Inside Header.js

import {useEffect,useState} from "react";
import someFunction from "./someFile.js";

const Header = () => {
      const [text, setText] = useState("");

  useEffect(() => {
    someFunction()
      .then((data) => setText(data))
      .catch((err) => console.log(err));
  }, []);

    return (<h1>{text}</h1>)
}

export default Header;
0
0

Remember the concept of immutability in React.js, you should do something like this:



import someFunction from "./someFile.js";


import { useState, useEffect, useCallback } from ‘react’;


const Header = () => {
   const [text, setText] = useState(‘’);

   const getApiText = useCallback(async () => {
     let apiReturnText = await someFunction();
     setText(apiReturnText);
   }, [])

    useEffect(() => {
      getApiText(); 
    }, [getApiText])

    return (<h1>{text}</h1>)
}

export default Header;
2
  • this code won't work well either, because getApiText is redefined on each render so the useEffect will be called on each render too - hence continually. Either make the dependency array of the useEffect empty (if you don't mind linter warnings about missing dependencies) or better, wrap getApiText in useCallback so it doesn't change. Commented Apr 26, 2022 at 20:35
  • You're right Robin, i thought useCallback is going to make things a little bit hard to understand, but i forgot the problem that you bring, thanks for pointing that! I've already fix Commented Apr 26, 2022 at 20:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.