Image URL in React
I am using the same image URL structure for every component in my application, however, one of them shows my image URL wrong. It basically adds 'menu' in front of the image URL.
Correct URL: /images/pizza.png
Rendered URL: /menu/images/pizza.png
I have a menu and detail component. When I click on a dish on the menu, I show the details component which is related to the dish.
Before rendering my image, I printed out the URL of the image, it shows the correct URL(without 'menu' string), however, after renders it, 'menu' is added in front of the image URL and the image is not shown.
Menu Component: (It shows image URL correctly)
function RenderMenuItem({ dish }) {
return(
<Card>
<Link to={`/restaurant/menu/${dish.id}`}>
<CardImg width="100%" src={dish.image} alt={dish.name} />
<CardImgOverlay>
<CardTitle>{dish.name}</CardTitle>
</CardImgOverlay>
</Link>
</Card>
);
}
Detail Component: (It shows image URL wrong)
function RenderDish({ dish }) {
if (dish) {
return (
<FadeTransform in
transformProps={{
exitTransform: 'scale(0.5) translateY(-50%)'
}}>
<Card>
<CardImg width="100%" src={dish.image} alt={dish.name} />
<CardBody>
<CardTitle>{dish.name}</CardTitle>
<CardText>{dish.description}</CardText>
</CardBody>
</Card>
</FadeTransform>
)
} else {
return (
<div></div>
)
}
}
Related routes:
<Route exact path="/restaurant/menu" component={() => <Menu dishes=
{this.props.dishes} /> } />
<Route path="/restaurant/menu/:dishId" component={DishWithId} />
reactjs url
add a comment |
I am using the same image URL structure for every component in my application, however, one of them shows my image URL wrong. It basically adds 'menu' in front of the image URL.
Correct URL: /images/pizza.png
Rendered URL: /menu/images/pizza.png
I have a menu and detail component. When I click on a dish on the menu, I show the details component which is related to the dish.
Before rendering my image, I printed out the URL of the image, it shows the correct URL(without 'menu' string), however, after renders it, 'menu' is added in front of the image URL and the image is not shown.
Menu Component: (It shows image URL correctly)
function RenderMenuItem({ dish }) {
return(
<Card>
<Link to={`/restaurant/menu/${dish.id}`}>
<CardImg width="100%" src={dish.image} alt={dish.name} />
<CardImgOverlay>
<CardTitle>{dish.name}</CardTitle>
</CardImgOverlay>
</Link>
</Card>
);
}
Detail Component: (It shows image URL wrong)
function RenderDish({ dish }) {
if (dish) {
return (
<FadeTransform in
transformProps={{
exitTransform: 'scale(0.5) translateY(-50%)'
}}>
<Card>
<CardImg width="100%" src={dish.image} alt={dish.name} />
<CardBody>
<CardTitle>{dish.name}</CardTitle>
<CardText>{dish.description}</CardText>
</CardBody>
</Card>
</FadeTransform>
)
} else {
return (
<div></div>
)
}
}
Related routes:
<Route exact path="/restaurant/menu" component={() => <Menu dishes=
{this.props.dishes} /> } />
<Route path="/restaurant/menu/:dishId" component={DishWithId} />
reactjs url
Add someconsole.log()and figure out why it's not being passed correctly. My guess is you forgot a/at the start to mark the path as absolute.
– FrankerZ
Dec 28 '18 at 23:52
Have you tried./imgPathinstead of/imgPath?
– L. Norman
Dec 28 '18 at 23:54
I didn't forget a/. I really don't know where the problem is.
– newbie35
Dec 28 '18 at 23:56
@L.Norman The problem should be specific. I render a lot of images in the same way, but the problem happens in only the Detail Component and somehow it could be related Menu component. I added related routes to my post.
– newbie35
Dec 28 '18 at 23:58
Pretty sure this is caused by the image url itself. It is taken as relative from current path and not from the root. That means it is missing/at the start.
– Sulthan
Dec 29 '18 at 0:03
add a comment |
I am using the same image URL structure for every component in my application, however, one of them shows my image URL wrong. It basically adds 'menu' in front of the image URL.
Correct URL: /images/pizza.png
Rendered URL: /menu/images/pizza.png
I have a menu and detail component. When I click on a dish on the menu, I show the details component which is related to the dish.
Before rendering my image, I printed out the URL of the image, it shows the correct URL(without 'menu' string), however, after renders it, 'menu' is added in front of the image URL and the image is not shown.
Menu Component: (It shows image URL correctly)
function RenderMenuItem({ dish }) {
return(
<Card>
<Link to={`/restaurant/menu/${dish.id}`}>
<CardImg width="100%" src={dish.image} alt={dish.name} />
<CardImgOverlay>
<CardTitle>{dish.name}</CardTitle>
</CardImgOverlay>
</Link>
</Card>
);
}
Detail Component: (It shows image URL wrong)
function RenderDish({ dish }) {
if (dish) {
return (
<FadeTransform in
transformProps={{
exitTransform: 'scale(0.5) translateY(-50%)'
}}>
<Card>
<CardImg width="100%" src={dish.image} alt={dish.name} />
<CardBody>
<CardTitle>{dish.name}</CardTitle>
<CardText>{dish.description}</CardText>
</CardBody>
</Card>
</FadeTransform>
)
} else {
return (
<div></div>
)
}
}
Related routes:
<Route exact path="/restaurant/menu" component={() => <Menu dishes=
{this.props.dishes} /> } />
<Route path="/restaurant/menu/:dishId" component={DishWithId} />
reactjs url
I am using the same image URL structure for every component in my application, however, one of them shows my image URL wrong. It basically adds 'menu' in front of the image URL.
Correct URL: /images/pizza.png
Rendered URL: /menu/images/pizza.png
I have a menu and detail component. When I click on a dish on the menu, I show the details component which is related to the dish.
Before rendering my image, I printed out the URL of the image, it shows the correct URL(without 'menu' string), however, after renders it, 'menu' is added in front of the image URL and the image is not shown.
Menu Component: (It shows image URL correctly)
function RenderMenuItem({ dish }) {
return(
<Card>
<Link to={`/restaurant/menu/${dish.id}`}>
<CardImg width="100%" src={dish.image} alt={dish.name} />
<CardImgOverlay>
<CardTitle>{dish.name}</CardTitle>
</CardImgOverlay>
</Link>
</Card>
);
}
Detail Component: (It shows image URL wrong)
function RenderDish({ dish }) {
if (dish) {
return (
<FadeTransform in
transformProps={{
exitTransform: 'scale(0.5) translateY(-50%)'
}}>
<Card>
<CardImg width="100%" src={dish.image} alt={dish.name} />
<CardBody>
<CardTitle>{dish.name}</CardTitle>
<CardText>{dish.description}</CardText>
</CardBody>
</Card>
</FadeTransform>
)
} else {
return (
<div></div>
)
}
}
Related routes:
<Route exact path="/restaurant/menu" component={() => <Menu dishes=
{this.props.dishes} /> } />
<Route path="/restaurant/menu/:dishId" component={DishWithId} />
reactjs url
reactjs url
edited Dec 29 '18 at 0:00
newbie35
asked Dec 28 '18 at 23:21
newbie35newbie35
13
13
Add someconsole.log()and figure out why it's not being passed correctly. My guess is you forgot a/at the start to mark the path as absolute.
– FrankerZ
Dec 28 '18 at 23:52
Have you tried./imgPathinstead of/imgPath?
– L. Norman
Dec 28 '18 at 23:54
I didn't forget a/. I really don't know where the problem is.
– newbie35
Dec 28 '18 at 23:56
@L.Norman The problem should be specific. I render a lot of images in the same way, but the problem happens in only the Detail Component and somehow it could be related Menu component. I added related routes to my post.
– newbie35
Dec 28 '18 at 23:58
Pretty sure this is caused by the image url itself. It is taken as relative from current path and not from the root. That means it is missing/at the start.
– Sulthan
Dec 29 '18 at 0:03
add a comment |
Add someconsole.log()and figure out why it's not being passed correctly. My guess is you forgot a/at the start to mark the path as absolute.
– FrankerZ
Dec 28 '18 at 23:52
Have you tried./imgPathinstead of/imgPath?
– L. Norman
Dec 28 '18 at 23:54
I didn't forget a/. I really don't know where the problem is.
– newbie35
Dec 28 '18 at 23:56
@L.Norman The problem should be specific. I render a lot of images in the same way, but the problem happens in only the Detail Component and somehow it could be related Menu component. I added related routes to my post.
– newbie35
Dec 28 '18 at 23:58
Pretty sure this is caused by the image url itself. It is taken as relative from current path and not from the root. That means it is missing/at the start.
– Sulthan
Dec 29 '18 at 0:03
Add some
console.log() and figure out why it's not being passed correctly. My guess is you forgot a / at the start to mark the path as absolute.– FrankerZ
Dec 28 '18 at 23:52
Add some
console.log() and figure out why it's not being passed correctly. My guess is you forgot a / at the start to mark the path as absolute.– FrankerZ
Dec 28 '18 at 23:52
Have you tried
./imgPath instead of /imgPath?– L. Norman
Dec 28 '18 at 23:54
Have you tried
./imgPath instead of /imgPath?– L. Norman
Dec 28 '18 at 23:54
I didn't forget a
/. I really don't know where the problem is.– newbie35
Dec 28 '18 at 23:56
I didn't forget a
/. I really don't know where the problem is.– newbie35
Dec 28 '18 at 23:56
@L.Norman The problem should be specific. I render a lot of images in the same way, but the problem happens in only the Detail Component and somehow it could be related Menu component. I added related routes to my post.
– newbie35
Dec 28 '18 at 23:58
@L.Norman The problem should be specific. I render a lot of images in the same way, but the problem happens in only the Detail Component and somehow it could be related Menu component. I added related routes to my post.
– newbie35
Dec 28 '18 at 23:58
Pretty sure this is caused by the image url itself. It is taken as relative from current path and not from the root. That means it is missing
/ at the start.– Sulthan
Dec 29 '18 at 0:03
Pretty sure this is caused by the image url itself. It is taken as relative from current path and not from the root. That means it is missing
/ at the start.– Sulthan
Dec 29 '18 at 0:03
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53965333%2fimage-url-in-react%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53965333%2fimage-url-in-react%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Add some
console.log()and figure out why it's not being passed correctly. My guess is you forgot a/at the start to mark the path as absolute.– FrankerZ
Dec 28 '18 at 23:52
Have you tried
./imgPathinstead of/imgPath?– L. Norman
Dec 28 '18 at 23:54
I didn't forget a
/. I really don't know where the problem is.– newbie35
Dec 28 '18 at 23:56
@L.Norman The problem should be specific. I render a lot of images in the same way, but the problem happens in only the Detail Component and somehow it could be related Menu component. I added related routes to my post.
– newbie35
Dec 28 '18 at 23:58
Pretty sure this is caused by the image url itself. It is taken as relative from current path and not from the root. That means it is missing
/at the start.– Sulthan
Dec 29 '18 at 0:03