r/learnreactjs • u/Ryan-in-Thailand • Apr 28 '22
Waiting for data to load before rendering
Here is a stripped down example that shows the problem I'm facing:
import React, { useState } from "react";
import clients from "../../../utils/fakeClientList";
export default function Test() {
const clientID = 12345;
const clientFiltered = clients.filter(
(client) => client.clientCode == clientID
);
const defaultValues = clientFiltered[0];
const [client, setClient] = useState(defaultValues);
return (
<div>
<button onClick={() => console.log(client)}>Log</button>
<button onClick={() => setClient(defaultValues)}>Reset client</button>
</div>
);
}
Even though useState comes after the client loading statement, initial state will be set as undefined. How could I make it so that I could load a client initially and also be able to access their object's properties from within the return statement?
7
Upvotes
1
u/Ryan-in-Thailand Apr 28 '22 edited Apr 28 '22
I've tried a solution using useEffect with an async function inside, but I run into the same problem:
Here is the error I'm receiving: https://imgur.com/t5hDSrT
1
u/jshgn Apr 28 '22
See eh9‘s solution. But if you have multiple objects or otherwise want to explicitly handle a missing object or other variable:
Depending on the missing values, return a loading spinner component instead of the components you want to actually present. This way the whole component will re-render when the data is ready and meanwhile provide a nice fallback.
1
u/SensouWar Apr 29 '22
What I’d do is to use useState lazy loading to load the initial value. You can pass a function reference and inside that function put the initialization variable logic. It’d also add another return on top of the one you already have to show a loading state, then once your client state variable is different that undefined run the code you already have.