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
Add a ment  | 

2 Answers 2

Reset to default 3

When 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