D
D
drunkseal2021-04-27 17:47:30
React
drunkseal, 2021-04-27 17:47:30

Request for backing before loading the site?

You need to make a request for backing.
When I use useEffect the site blinks, how can I avoid it?
Example from my code:
const Container = (...) => {
const dispatch = useDispatch()

useEffect(() => {
dispatch(fetchVacations())
}, [])

return (...)}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolay Matyushkin, 2021-04-27
@Devilz_1

1) useLayoutEffect - you can try it, but react architects strongly recommend using useEffect
2) if we listen to react architects, then we create a variable in the component state: Before returning jsx markup, we set a condition, if isLoading === false, then return. And in useEffect, after the dispatch, this is set to true in isLoading.
const [isLoading, setLoading] = useState(false);

useEffect(() => {
dispatch(fetchVacations());
if (!isLoading) setLoading(true);   // Сэтим истину только в случае если isLoading === false 
}, [])                              // во избежании мультирендеринга

if (!isLoading) return;

return (...)}

thus, the component is not rendered until we dispatch the data to the side

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question