admin管理员组文章数量:1291198
Im trying to return a div dynamically from a function in react. Im calling a function to render out directly in the ponents render function, but it does not render out. Im sure its a simple error in the code but I cant spot it.
The code:
import React, { Component } from 'react'
import ProgressBar from './ProgressBar'
require('styles/_servicesPage/priceCalc.css')
export default class PriceCalculator extends Component {
ponentWillMount() {
this.state = {
arr: []
}
}
minimizeDiv() {
this.props.toggleDiv(false)
}
returnDiv(){
return
<div>
<p>
Printing out text!
</p>
</div>
}
render() {
var styleVar = {
backgroundImage: 'url(assets/images/services/pricecalc_blue_bg.svg)',
backgroundPosition: 'right center'
}
return (
<div className="service-form" id="priceCalc" style={styleVar}>
<div>
<h1>Priskalkyl för badrum</h1>
<p>
Välkommen att fylla i formuläret,
så är du ett steg närmare dina drömmars badrum.
</p>
</div>
{this.returnDiv}
<div onClick={this.minimizeDiv.bind(this)} className="minimizeBorder">
<img src="assets/arrows/minimizeArrow.svg"/>
<p>Minimera</p>
<img src="assets/arrows/minimizeArrow.svg"/>
</div>
</div>
)
}
}
Im trying to return a div dynamically from a function in react. Im calling a function to render out directly in the ponents render function, but it does not render out. Im sure its a simple error in the code but I cant spot it.
The code:
import React, { Component } from 'react'
import ProgressBar from './ProgressBar'
require('styles/_servicesPage/priceCalc.css')
export default class PriceCalculator extends Component {
ponentWillMount() {
this.state = {
arr: []
}
}
minimizeDiv() {
this.props.toggleDiv(false)
}
returnDiv(){
return
<div>
<p>
Printing out text!
</p>
</div>
}
render() {
var styleVar = {
backgroundImage: 'url(assets/images/services/pricecalc_blue_bg.svg)',
backgroundPosition: 'right center'
}
return (
<div className="service-form" id="priceCalc" style={styleVar}>
<div>
<h1>Priskalkyl för badrum</h1>
<p>
Välkommen att fylla i formuläret,
så är du ett steg närmare dina drömmars badrum.
</p>
</div>
{this.returnDiv}
<div onClick={this.minimizeDiv.bind(this)} className="minimizeBorder">
<img src="assets/arrows/minimizeArrow.svg"/>
<p>Minimera</p>
<img src="assets/arrows/minimizeArrow.svg"/>
</div>
</div>
)
}
}
Share
Improve this question
asked Sep 21, 2016 at 19:14
AlfredOdlingAlfredOdling
4132 gold badges6 silver badges20 bronze badges
2
-
2
You're not calling a function. Replace
{this.returnDiv}
with{this.returnDiv()}
– Matis Lepik Commented Sep 21, 2016 at 19:16 - I have tried with {this.returnDiv()} aswell. Also tried the first alternative. I does not even show in the element inspector! – AlfredOdling Commented Sep 21, 2016 at 21:01
2 Answers
Reset to default 3When you render out a div via a function the intendation and placement of the div matters.
renderDiv() {
return (<div>
</div>)
}
Works but not
renderDiv() {
return
(<div>
</div>)
}
call returnDiv, also keep your returned multiline jsx inside parenthesis, else it might return null;
import React, { Component } from 'react'
import ProgressBar from './ProgressBar'
require('styles/_servicesPage/priceCalc.css')
export default class PriceCalculator extends Component {
ponentWillMount() {
this.state = {
arr: []
}
}
minimizeDiv() {
this.props.toggleDiv(false)
}
returnDiv(){
return (
<div>
<p>
Printing out text!
</p>
</div>
)
}
render() {
var styleVar = {
backgroundImage: 'url(assets/images/services/pricecalc_blue_bg.svg)',
backgroundPosition: 'right center'
}
var returnDiv = this.returnDiv();
return (
<div className="service-form" id="priceCalc" style={styleVar}>
<div>
<h1>Priskalkyl för badrum</h1>
<p>
Välkommen att fylla i formuläret,
så är du ett steg närmare dina drömmars badrum.
</p>
</div>
{returnDiv}
<div onClick={this.minimizeDiv.bind(this)} className="minimizeBorder">
<img src="assets/arrows/minimizeArrow.svg"/>
<p>Minimera</p>
<img src="assets/arrows/minimizeArrow.svg"/>
</div>
</div>
)
}
}
本文标签: javascriptReturn and render a div in ReactStack Overflow
版权声明:本文标题:javascript - Return and render a div in React - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741506886a2382373.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论