N
N
n1ksON2020-07-01 20:19:09
React
n1ksON, 2020-07-01 20:19:09

How to update url after API request?

I'm using react-router-dom.
The bottom line: a request is made to the API, after receiving the response, everything is displayed, but it is necessary that after receiving the response, the url link is updated. Since the page uses pagination, when you click on each button on the page, the url should change. So that later there was access by url to each page. How to do it?
At first I wanted to use history.pushState(), but I realized that when using react-router-dom this is redundant.
Now I do this:
*A request is executed*
*An answer

is received* The request itself is written into a constant and it is substituted into NavLink and Route. But nothing comes out.
Example:
app.js

const link = props.link // в props.link приходит: `?q=+language:&sort=stars&order=desc&page=1&per_page=10`
<Route path={link} />

Main.js
<input />
<NavLink to={props.link}><button>Search</button></NavLink>

Mistake:
TypeError: Cannot read property 'pathname' of undefined

Tell me what to do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
cython, 2020-07-01
@n1ksON

import { useHistory } from "react-router-dom";

const history = useHistory();  // В компоненте

history.push("/path");  // Чтоб перейти к Route через react-router

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question