组件内部的map函数

时间:2019-03-17 04:02:16

标签: javascript reactjs lodash

我正在尝试在ContentItem组件中应用map函数。 ContentItem从Content组件接收道具,它是一个带有数据的滑块,为此我使用lodash来获取要传递的信息。 我只会得到很多内容相同的行。

这是我的代码:

content.js

export default class Content extends React.Component {
  constructor(props){
    super(props)
    this.state = {
      picture : []
    }
  }

  componentDidUpdate(){
    const elements = _
      .chain(this.props.contentInfo)
      .filter(item => item)
      .flatMap('data')
      .filter(item => item)
      .value()

    if (this.state.picture == '') {
      this.setState({picture: elements})
    }
  }

  render() {
    <section className="content-group-wide-item">
      <div className='news-rail-container col-xs-12 col-sm-12 col-md-12 col-lg-12'>
        <Slider {...settings}>
          {
            this.state.picture.map((element , index) => (
              <ContentGroupWidePicture key={index} contentPicture={this.state.picture}/>
            ))
          }
        </Slider>
      </div>
    </section>
  }
}

component-item.js

export default function ContentItem({contentPicture}) {


     const elementos = _
      .chain(contentPicture)
      .map(item => _.pick(item, ['name', 'title', 'description']))
      .value()

      return (
        <React.Fragment>
                {
                elementos.map((element , index) => (
                <div className='content'>
                    <img alt='' src='https://dz92jh1unkapm.cloudfront.net/accounts/5bbcf78db827ce76aa82a6a0/uploads/railWide/5c754da1c8c1c50c88a2efc0-1551191457-w' className="img-responsive" />
                    <div className="info">
                            <div className="bottom">
                            <p className='news-title'>{element.name}</p>
                            <p className='news-subtitle'>Caption</p>
                        </div> 
                    </div>
                </div>
            ))
        }
    </React.Fragment>

}

这就是结果... enter image description here

1 个答案:

答案 0 :(得分:1)

此字符串中包含一个硬编码的图像源

<img alt='' src='https://dz92jh1unkapm.cloudfront.net/accounts/5bbcf78db827ce76aa82a6a0/uploads/railWide/5c754da1c8c1c50c88a2efc0-1551191457-w' className="img-responsive" />

这就是为什么当您迭代时都拥有全部相同的图像。

如果希望它是动态的,则应将指向图像的链接存储在picture数组中,并将其向下传递到ContentItem组件。

这是一个示例(我突出显示了更改的部分):

content.js

export default class Content extends React.Component {
  constructor(props){
    super(props)
    this.state = {
      picture : []
    }
  }

  componentDidUpdate(){
    const elements = _
      .chain(this.props.contentInfo)
      .filter(item => item)
      .flatMap('data')
      .filter(item => item)
      .value()

    if (this.state.picture == '') {
      this.setState({picture: elements})
    }
  }

  render() {
    <section className="content-group-wide-item">
      <div className='news-rail-container col-xs-12 col-sm-12 col-md-12 col-lg-12'>
        <Slider {...settings}>
          {
            this.state.picture.map((element , index) => (
              <ContentGroupWidePicture key={index} contentPicture={element}/> 
              //                                                   ^ PASS actual picture from picture array
            ))
          }
        </Slider>
      </div>
    </section>
  }
}

component-item.js

export default function ContentItem({contentPicture}) {


     const elementos = _
      .chain(contentPicture)
      .map(item => _.pick(item, ['name', 'title', 'description']))
      .value()

      return (
        <React.Fragment>
                {
                elementos.map((element , index) => (
                <div className='content'>
                    <img alt='' src={contentPicture} className="img-responsive" /> 
                    //               ^ USE received image url string as a src attribute 
                    <div className="info">
                            <div className="bottom">
                            <p className='news-title'>{element.name}</p>
                            <p className='news-subtitle'>Caption</p>
                        </div> 
                    </div>
                </div>
            ))
        }
    </React.Fragment>

}