I have this as my render function and I'm trying to pass the "{this.state.poster}" through it so it will load what ever random film poster it has received from the api. the console.log will show as /q2Y2EuDSaNCH88ETlyiu8bZc5TT.jpg for example but when I try to add that to the image with {this.state.poster} it doesn't work and im not sure why.
render() {
console.log(this.state.poster);
return (
<div className="main">
<h1> Lets see what you got</h1>
<div name="title">
{this.state.title}
</div>
<div name="overview">
{this.state.overview}
</div>
<div name="poster_path">
<img src="https://image.tmdb.org/t/p/original/${this.state.poster}" alt="picture" />
</div>
<div name="vote_average">
Vote:{this.state.vote}
</div>
<button onClick={this.getFilm}>Get Random Movie</button>
</div>
);
}
}
export default App;
first the curly brace specifying a js expression then string interpolation :)
<img src={`https://image.tmdb.org/t/p/original/${this.state.poster}`} alt="picture" />
string interpolation only works inside js expressions ;).
The ${expression} syntax is meant to be used in template literals
What you want here is to concatenate the state with the beginning of the image url like this :
<img
src={'https://image.tmdb.org/t/p/original/' + this.state.poster}
alt="picture"
/>
or by using a method like
getImgUrl (image) {
return 'https://image.tmdb.org/t/p/original/' + image;
}
and using it with
<img
src={this.getImgUrl(this.state.poster)}
alt="picture"
/>
You say that console.log prints out /q2Y2EuDSaNCH88ETlyiu8bZc5TT.jpg.
There is a backslash as the first character.
That tells me that the src attribute's value will be https://image.tmdb.org/t/p/original//q2Y2EuDSaNCH88ETlyiu8bZc5TT.jpg.
Note, the // to the right of original.
Try removing the extra backslash like following:
https://image.tmdb.org/t/p/original${this.state.poster}
Related
Hi I am trying to detect when a div is clicked that is nested inside of a container.
the goal is to extract the src for an image as a variable.
I want the path to the image
Would this work ?
<div className = "container">
<div onClick{divClicked()} className="column">
<img alt="" src={props.gif} className="ui image" />
</div>
</div>
divClicked() {
console.log(props.gif)
}
You cannot set an onclick like that.
<div onClick={divClicked} className="column">
this code should work if the divClicked function is in the same context.
If this function is a member of a class, you might want to use this.divClicked
Additionally if you want to pass parameters to the function you can use arrow functions to do this.
<div onClick={()=>divClicked(someParam)} className="column">
I have a problem with an token template in 2sxc V.8.4.5 on DNN 7.4.2. It's a template with support ListContent and ListPresentation:
<div class="row">
<div class="col-md-12">[ListContent:Toolbar]
<h[ListContent:Presentation:TitleHTag] style="text-align:[ListContent:Presentation:TitleTextAlign];">[ListContent:Titel]</h[ListContent:Presentation:TitleHTag]>
<p style="text-align:[ListContent:Presentation:TextTextAlign];">[ListContent:Text]</p>
<repeat>
<div class="col-md-[Content:Presentation:BootstrapWidthContent]">[Content:Toolbar]
<div>
<p><img src="[Content:Image]" style="float:[Content:Presentation:CssImageFloat];margin-top:8px; width:[Content:Presentation:CssWidthImage]"/></p>
</div>
<h[Content:Presentation:Title1HTag] class="[Content:Presentation:CssClassColorTitle]">[Content:Title]</h[Content:Presentation:Title1HTag]>
<h[Content:Presentation:Title2HTag] class="[Content:Presentation:CssClassColorTitle2]">[Content:SubTitle]</h[Content:Presentation:Title2HTag]>
<div class="[Content:Presentation:CssClassColorText]">[Content:Text]<br /><br />
<a class="[Content:Presentation:CssClassColorLinkText]" href="[Content:Link]">[Content:LinkText]</a>
</div>
</div>
</repeat>
</div>
</div>
The problem are at the lines:
<h[Content:Presentation:Title1HTag] class="[Content:Presentation:CssClassColorTitle]">[Content:Title]</h[Content:Presentation:Title1HTag]>
<h[Content:Presentation:Title2HTag] class="[Content:Presentation:CssClassColorTitle2]">[Content:SubTitle]</h[Content:Presentation:Title2HTag]>
Some tokens in that lines are not replaced with values. i'm getting
<h class="titlered">Title1<h>
<h class="titlered">Title2<h>
It cannot be a problem with Content:Presentation, because the other tokens from this presentation are correct replaced.
Any hints?
I have to guess a bit, but I'm thinking of some kind of spelling / naming issues. Are you sure, that the field is really called Title1HTag, or could it be a misspelling like Titel1HTag or TitleH1Tag?
I recommend that you try to output the value somewhere else just to look at it, like
XXX[tag-to-test]YYY
I only want to display the image div if the object returns an image, right now I'm outputting the image in my <div class="image">, however, if there's no image, then the <div class="image"> should not output:
<div class="image">
<img src="{{item.logo}}" alt="{{item.title}}" title="{{item.title}}" />
</div>
How about this?
<div class="image" ng-if="item">
...
</div>
See the documentation for ngIf.
You could also use ng-show, which will merely hide the div instead of removing it completely. See the documentation for ngShow.
React is doing strange things with the <p> tag. Using the same markup structure, with a <p> tag vs a <div> tag produces very different results. For example,
var withP = (
<p>
withP
<div />
</p>
);
var withDiv = (
<div>
withDiv
<div />
</div>
);
Here is what the generated markup looks like in chrome:
Here is a live jsbin demo.
Why does React render <p> differently than <div>?
<p> can not have nested block elements. Chrome (not React) is transforming the markup to make it valid.
Regarding the use of ng-src in order to display an image, this code works during runtime - but not on the initial page load:
<div class="imageHolder" ng-click="openWidgetSettings(widget);" ng-show="widget.showInitImage">
<img ng-src="../../Images/{{widget.initImage}}" />
<div class="caption">Click to configure</div>
</div>
on my initial page load I get the error:
GET http://localhost:33218/Images/ 403 (Forbidden)
Yet during runtime, when I drag and drop an image onto my dashboard, the front end doesn't complain anymore.
I do realize that the dashboard framework I'm using is dynamically adding a div onto my page, and then rendering the image; however, why does it NOT complain at this time ?
In other words, I'm trying to avoid using the full path like this:
<img ng-src="http://localhost:33218/Images/{{widget.initImage}}" />
**** UPDATE ****
This bit of code works, and I did not need to specify ".../../" relative path.
<div class="imageHolder" ng-click="openWidgetSettings(widget);" ng-hide="widget.gadgetConfigured">
<img ng-src="Images/{{widget.initImage}}" />
<div class="caption">Click to configure</div>
</div>
In addition, my {{widget.initImage}} was coming back empty upon reload - an application bug !
Change you code to following.
You need to check widget.initImage is initialized or not. Before passing it to ng-src .
Use ng-if on widget.initImage
<div class="imageHolder" ng-click="openWidgetSettings(widget);" ng-show="widget.showInitImage">
<img ng-src="../../Images/{{widget.initImage}}" ng-if="widget.initImage" />
<div class="caption">Click to configure</div>
</div>
I'd suggest you to use ng-init directive like this...
<div class="imageHolder" ng-click="openWidgetSettings(widget);" ng-show="widget.showInitImage" ng-init="getImgUrl()">
<img ng-src="{{myImgUrl}}" />
<div class="caption">Click to configure</div>
</div>
In your controller,
$scope.getImgUrl=function()
{
$scope.myImgUrl= //get your img url whatever it is...
// You can also set widget.showInitImage variable here as well...
}