import React from 'react';
import { useParams, useLocation, useHistory } from 'react-router-dom';
import qs from 'qs';
function MyUrl () {
const history = useHistory();
function changeUrl () {
history.push({
pathname: '/myurl/2',
search: 'name=chris',
state: { fromSource: 1 },
})
}
const params = useParams();
const { id } = params;
const location = useLocation();
const { search, state = {} } = location;
const { name } = qs.parse(search.replace(/^\?/, ''));
const { fromSource } = state;
return (
<div>
<div>
myurl id: { id },<br/>
search name: { name },<br />
state fromSource: { fromSource },<br />
</div>
<div onClick={ changeUrl }>
change url
</div>
</div>
)
}
export default MyUrl;
const route = {
path: '/myurl/:id',
component: myUrl,
};
<Route path={route.path} component={route.component} />
