【问题标题】:How to handle onClick to many objects from the same array如何处理来自同一数组的多个对象的 onClick
【发布时间】:2016-07-18 15:58:01
【问题描述】:

我遇到了一些问题,并且有点生气。我想 .map 我的对象数组。并使每个人都可以点击。单击后,我希望特定对象仅显示其头像:

const stations = [
  {
    name: 'first',
    avatar: './img/1.jpg'
  },
  {
    name: 'second',
    avatar: './img/2.jpg'
  },
  {
    name: 'third',
    avatar: './img/3.jpg'
  },
  {
    name: 'fourth',
    avatar: './img/4.jpg'
  },
  {
    name: 'fifth',
    avatar: './img/5.jpg'
  }
]

现在。我可以从我的数据库数组中访问我需要的值。但!我有一个问题:

this.state = {
      clicked: false
    }
    this.handleClick = this.handleClick.bind(this)
}

我的对象没有单独的状态。因此,当我想基于 this.state 创建一些操作(例如隐藏和显示)时,它始终适用于每个元素。

我的代码以某种方式工作。当我呈现列表并单击任何按钮时,每个按钮都会发生操作:

class radiosList extends React.Component {
  constructor() {
    super();
    this.state = {
      clicked: false
    }
    this.handleClick = this.handleClick.bind(this)
  }
  handleClick(station) {
    console.log(this)
    this.setState({ clicked: !this.state.clicked })

  }
  render () {

     return (
      <div>
        {
          this.props.stations.map((station, index) => (
            <div className='radio_list radio_list--component' style={{cursor: 'pointer'}} onClick={() => this.handleClick(station.avatar)}>
              <div className='radio_list radio_list--component radio_list--component-station_name'>{station.name}</div>
            </div> 
          ))
        }
      </div>
    )
  }
}

export default radiosList

编辑:第一个答案有助于访问我需要的值。

【问题讨论】:

  • 您是否尝试过更改imgSrc? {this.state.clicked ? :空}
  • 工作,但我做了一些不好的描述 => 现在是正确的一个:)

标签: javascript object reactjs onclick


【解决方案1】:

这是通过向数据添加额外的clicked 状态属性来实现所需的方式。可能有更好的方法,但到目前为止,我就是这样做的。

class radiosList extends React.Component {
  constructor() {
    super();
    this.state = {
      data: [],
    }
    this.handleClick = this.handleClick.bind(this)
  }
  handleClick(index) {
    console.log(this)
    var data = [...this.state.data];
    data[index].clicked = !data[index].clicked;
    this.setState({data});

  }
  render () {
    var self = this;
     this.props.station.forEach(function(station) {
       self.state.data.push({name: station.name, avatar: station.avatar, clicked: false});
       self.setState({data: self.state.data});
     })
     return (
      <div>
        {
          this.state.data.map((station, index) => (
            <div className='radio_list radio_list--component' style={{cursor: 'pointer'}} onClick={() => this.handleClick(index)}>
              <div className='radio_list radio_list--component radio_list--component-station_name'>{station.name}</div>
            </div> 
          ))
        }
      </div>
    )
  }
}

export default radiosList

【讨论】:

  • 这对我来说没有意义。你想从空的数据数组中渲染?因为点击后任何东西都会添加到 DATA 中,
  • 数据数组怎么是空的。道具站的内容作为对象数组复制到数据中,单击时带有附加参数
  • 但它不起作用。我得到“未捕获的类型错误:无法读取未定义的属性'状态'”仍在尝试解决问题
  • @Draer 我刚刚意识到我在代码中犯了一个小错误。对不起这个错误。在 forEach 循环中,我写了这个语句 {data: this.state.data} 而它应该是 {data: self.state.data}。我已经编辑了代码。你可以检查一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多