I have the following code and it is causing my Dashboard
component to render a few times. How can I reduce it to only once?
Dashboard
const Dashboard: React.FC = () => {
const { account } = useActiveWeb3React();
const {
status: status1,
loading: loading1,
error: error1,
response: response1,
payload: payload1
} = useAllLPTokens()
... ...
Custom Hook
const useAllLPTokens = (): GraphQLResponse<LPTokens> => {
const [status, setStatus] = useState<number>(0);
const [loading, setLoading] = useState<boolean>(false);
const [error, setError] = useState<any>();
const [response, setResponse] = useState<any>();
const [payload, setPayload] = useState<LPTokens | undefined>();
const getLPTokenData = async () => {
setLoading(true);
try {
const res = await axios.post(subgraphEndpoint,
{
headers: { "Content-Type": "application/json" },
query: graphQuery
}
);
setStatus(res.status);
setResponse(res)
setPayload(res.data)
} catch (error) {
setError(error)
}
setLoading(false);
}
useEffect(() => {
getLPTokenData();
}, [])
return { status, loading, error, response, payload }
}
I’m guessing its because of all the useState
in the custom hook, how can I change my code?
2
Answers
I guess you can have a single hook that stores the state like this:
Doing this you are sure that UI batch updates but my advice is not to worry too much about renders and rerenders in React, the library itself is really smart to understand what actually changed between renders and update the DOM.
The code you posted is perfectly fine, I would not change it.
If you use React.strictMode, useEffect will run two times.
Suppose you are using un-strict Mode now.
If you add
in your Dashboard. you will see
Here you can delete loading status to reduce the second render. set loading component base on response
By the way, duplicate states(status、payload etc.) is useless, always make unexpected bug.