let { userName } = useParams();
/* ... */
<img src={`${window.location.origin}/components/Users/img/${userName}.jpg`} /> import johnsonImg from "./img/johnson.jpg"
...
<img src={johnsonImg} />
import { useRouteMatch, Link, useParams } from 'react-router-dom';
function UserId() {
let { userName } = useParams();
let userNameСap = userName[0].toUpperCase() + userName.substring(1);
let match = useRouteMatch();
let usersLink = match.url.split('/')[1];
let userNameImg = './img/' + useParams().userName +'.jpg'; // ИМПОРТ В КОМПОНЕНТЕ НЕ ДЕЛАЕТСЯ! КАК БЫТЬ???
return (
<>
<div className="container">
<h1>User: { userNameСap }</h1>
<img src={ userNameImg } alt={ userNameСap }/>
<Link to={ `/${usersLink}` }>Назад</Link>
</div>
</>
);
}
export default UserId;
import { useHistory } from "react-router-dom";
const history = useHistory()
return (
<button type="button" onClick={() => history.goBack()}>
Go back
</button>
);
import fooImg from "./img/foo.jpg"
import barImg from "./img/bar.jpg"
const getImg = (name) => {
switch (name)
{
case "foo": return fooImg;
case "bar": return barImg;
......
}
}