反应组件内的映射函数

问题描述 投票:1回答:1

我正在尝试在我的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>
  }
}

组件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

javascript reactjs lodash
1个回答
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>
  }
}

组件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>

}
热门问题
推荐问题
最新问题