Answer the question
In order to leave comments, you need to log in
Where to execute React asynchronous requests?
I have a react component, how do I implement an asynchronous request to the API (not in useEffect).
You need to send a request when the button is pressed, and then call the dispatch action'a, which will put the received data in the state (and in the same component where the button was pressed, draw the data from the state)
Answer the question
In order to leave comments, you need to log in
<code lang="javascript">
const reactCustomComponent = (props) => {
const data = useSelector((state) => state.data)
const onClickBtnHandler = (event) => {
const response = асинхронный запрос(axios || fetch)
dispatch action'a, (response.data) || .then(res => {
dispatch action'a, (response.data)
})
}
return (
<>
<Button onClick={onClickBtnHandler} />
{
data && data
}
</>
)
}
</code>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question