M
M
MagicMight2020-05-19 16:07:36
JavaScript
MagicMight, 2020-05-19 16:07:36

Rendering a component based on ajax request results?

There is data requested from the server via useEffect( () => {fetch(...)} ) (once)
There is a function that processes this data, fitting it to the structure of the component, and executes setState() to apply the changes and render.

I understand that useEffect is not very friendly with setState, so the question is how to organize the chain [data acquisition] -> [data processing] -> [setState]?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-05-19
@MagicMight

I understand that useEffect is not very friendly with setState, so the question is how to organize the chain [data acquisition] -> [data processing] -> [setState]?

useEffect is quite friendly with another useState hook
const [data, setData] = React.useState(null)

React.useEffect(() => {
  const fetchedData = /* как-то данные получить */
  setData(fetchedData)
}, [])

return data ? <p>{ JSON.stringify(data) } </p> : null

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question