如何在反应循环中生成换行符?

时间:2017-06-01 15:03:52

标签: javascript css reactjs

我有一个反应组件,我需要分成几行。它在我猜测的循环回报中产生了4-5盒li。我想要做的是在顶部有2个盒子,然后在它下面有3个盒子。因此,如果有4个盒子,它们将叠加2x2在彼此之上。如果有5个,2个在顶部,3个在底部。

所以问题是,如何在反应中的循环返回中生成换行符?

代码:

import React from 'react'
import classnames from 'classnames'

export default class Tile extends React.Component {
 constructor( props ) {
  super( props )
}

componentWillAppear( done ) {
// let rotateZ = `rotateZ( ${ ~~( ( Math.random() * -20 ) + 
Math.random() * 40 ) }deg )`
// let rotateX = `rotateX( ${ ~~( Math.random() * 80 ) }deg )`
// let scale = `scale( ${ .65 + Math.random() * .25 } )`
// let initialTransform = {
//   transform: `${ rotateZ } ${ rotateX } ${ scale }`
// }
// this.refs.container.style.transform = `${ rotateZ } ${ rotateX } ${ 
scale }`

this.refs.container.classList.add( 'Tile--isAppearing' )
done()
}

componentDidAppear() {
setTimeout( () => {
  // set visible and clear jitter
  this.refs.container.classList.remove( 'Tile--isAppearing' )
  this.refs.container.style.transform= ''
}, Math.random() * ( this.props.number * 250 ) )
}

onClick = event => {
const { id, value } = this.props
this.props.onClick( id, value )
}

render() {
// Add matrix jitter

let classes = classnames({
  'Tile': true,
  'Tile--isSelected': this.props.selected
})

return (

  <li ref="container" className={ classes } onClick={ this.onClick }>
    <span className="Tile-content">{ this.props.text }</span>
  </li>

)
  }
}

请求第二组代码:

render() {
let tiles = this.props.slide.answers
  .map( ( answer, index ) => {
    let id = 'answer' + index
    return <Tile
      key={ id }
      id={ id }
      value={ answer.value }
      text={ answer.text }
      selected={ this.selected.has( id ) }
      onClick={ this.onTileClick }
    />
  })

 let buttonClasses = classnames( 'Btn', 'Btn--isOrange', 'Btn--
 isContinue', {
  'u-transparent': !this.state.complete
})

   return (
  <div ref="main" className="js-main State">
    <p className="Question-Body">
      { this.props.slide.body }
    </p>
    <TransitionGroup { ...transitionProps } >
      { tiles }
    </TransitionGroup>
    <button ref="continue" className={ buttonClasses } onClick={ 
  this.onContinue }>{ this.props.slide.continue }</button>
  </div>
  )
  }

1 个答案:

答案 0 :(得分:1)

您需要手动插入换行符:

let tiles = this.props.slide.answers
  .map( ( answer, index ) => {
    let id = 'answer' + index
    let tile = <Tile
      key={ id }
      id={ id }
      value={ answer.value }
      text={ answer.text }
      selected={ this.selected.has( id ) }
      onClick={ this.onTileClick }
    />
    return index == 1 ? [tile, <br/>] : tile;
  })

这会将[tile, tile, tile, tile]变为[tile, [tile, <br/>], tile, tile]