首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在React中呈现组件onClick

在React中呈现组件onClick
EN

Stack Overflow用户
提问于 2016-11-01 17:07:33
回答 1查看 5.8K关注 0票数 1

我是新的反应和Javascript在总体上。这就是问题所在。

我在一个React组件中显示了一个图像列表。我试图实现的是定义一个方法来处理这些图像上的onClick方法,并将一个新的组件呈现为一个覆盖。这是我的密码:

代码语言:javascript
复制
class Viewer extends React.Component{

  constructor(props){
    super(props);
    this.state = {
      images : ImageList
    };

  }

  componentDidMount(){

  }
  handleClick(mediaId, event){
    event.preventDefault();
    render(){
      <MyNewComponent  mediaId=mediaId />
    }
  }

  render(){
    return (
      <div className="row">
        <div className="image-viewer">
          <ul className="list-inline">
            {this.state.images.map(function (image) {
              return (<li key={image.mediaId}><a href="#" onClick={this.handleClick.bind(image.mediaId, event)}><img src={image.src}
                                                               className="img-responsive img-rounded img-thumbnail"
                                                               alt={image.mediaId}/></a></li>);
            })}

          </ul>
        </div>
      </div>
    );
  }
}
export default Viewer;

这显然是错误的,并提出了一堆错误。有人能给我指明正确的方向吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-01 17:29:18

这里,如何处理click事件并显示覆盖

代码语言:javascript
复制
class Viewer extends React.Component{

  constructor(props){
    super(props);
    this.state = {
      images : ImageList,
      mediaId : 0
    };
    // Bind `this`
    // See "Binding to methods of React class" link below
    this.handleClick = this.handleClick.bind(this);
  }

  componentDidMount(){ }

  handleClick(event){
    event.preventDefault();
    // Get the value of the `data-id` attribute <a data-id="XX">
    var mediaId = event.currentTarget.attributes['data-id'].value;
    // Set the state to re render
    this.setState({ mediaId }); // ES6 shortcut
    // this.setState({ mediaId: mediaId }); // Without shortcut
  }

  render(){
    // Set a variable which contains your newComponent or null
    // See "Ternary operator" link below
    // See "Why use null expression" link below
    var overlay = this.state.mediaId ? <MyNewComponent  mediaId={this.state.mediaId} /> : null;

    return (
      <div className="row">
        <div className="image-viewer">
          { overlay }
          <ul className="list-inline">
            {this.state.images.map(image => {
              return (<li key={image.mediaId}><a href="#" onClick={this.handleClick} data-id={image.mediaId}><img src={image.src}
                                                               className="img-responsive img-rounded img-thumbnail"
                                                               alt={image.mediaId}/></a></li>);
            }).bind(this)}

          </ul>
        </div>
      </div>
    );
  }
}
export default Viewer;

文档

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40364992

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档