简体   繁体   English

如何获取我的handleClick渲染不同的组件

[英]How to get my handleClick to render different components

I am a beginner so please excuse my ignorance. 我是初学者,所以请原谅我的无知。 I am posting one component from a larger app that I am building. 我正在从正在构建的较大应用程序中发布一个组件。 This component has a handleClick function that changes the state when an image is clicked. 该组件具有handleClick函数,该函数可在单击图像时更改状态。 When the image is clicked, a new component is rendered. 单击图像后,将呈现一个新组件。 Currently, the same 'new component' is rendered no matter which image is clicked. 当前,无论单击哪个图像,都将渲染相同的“新组件”。 I'd like the component to be based on which image was clicked. 我希望组件基于单击的图像。

var AllocationDiv = React.createClass({
  getInitialState: function(){
    return {clicked: false};
  },
  handleClick: function() {
    this.setState (
    {clicked: true}
    ); 
  },
  render: function () {
    var handleFunc = this.handleClick; //because it wasn't brining this.handleClick into the render function
    var chartMap = pieCharts.map(function(prop){
      return <img onClick={handleFunc} id={prop} src={prop} />;
    }); 
    return (
      <div id='bottomSection'>
          <h2>Select Desired Asset Allocation</h2>
          <div id='pieCharts'>
            <table>
              <tr>{pieHeadMap}</tr>
            </table>
              <div>
                {chartMap}
                  <div id='test'>  
                    {this.state.clicked ? <TestComponent /> : null}
                  </div>
              </div>
            </div>
        </div>
    );
  }
});

var chartMap renders three images. var chartMap呈现三个图像。 Assuming I create three unique test components, how would I get them to be rendered depending on which image was clicked? 假设我创建了三个独特的测试组件,如何根据单击的图像来渲染它们? Here is the entire app. 这是整个应用程序。 I know the whole thing is a mess atm, but I'm using this as a sandbox to learn through problem-solving. 我知道整个过程都是一团糟,但是我将其用作沙盒,以学习解决问题的方法。 Thanks! 谢谢! http://codepen.io/sdpercussion/pen/NRQNLv?editors=0010 http://codepen.io/sdpercussion/pen/NRQNLv?editors=0010

So, here is what I would do for this. 所以,这就是我要做的。 Instead of having a boolean value for your clicked state, you should have a string. 您应该拥有一个字符串,而不是为您的clicked状态提供布尔值。 The string should be the name of the image being clicked. 该字符串应为被单击图像的名称。 (you need to assign names or ID's or anything to differentiate them) (您需要分配名称或ID或其他名称来区分它们)

so.. initial state is: 所以..初始状态是:

getInitialState: function(){
  return {clicked:''};
},

next your handleClick would have to change and you'd need to pass the image name/Id in to it. 接下来,您的handleClick必须进行更改,并且您需要将图像名称/ Id传递给它。

handleClick: function(image) {
  this.setState ({
    clicked: image
  }); 
},

then, inside your render.. 然后,在您的渲染中。

(make sure to .bind(this) in your map so you can use the component scope if you want to call your methods. var self = this; type workarounds show a misunderstanding of scope) (确保在地图中使用.bind(this) ,以便在您要调用方法时使用组件范围var self = this;类型变通办法表明对范围的误解)

render: function () {

    var chartMap = pieCharts.map(function(prop){
      // pass in your image name to your callback using bind, null value here skips over the scope portion and is what you need
      return <img onClick={this.handleClick.bind(null, prop)} id={prop} src={prop} />;
    }.bind(this)); 

    // get the component you want for each specific image and save to a variable for display
    var imgVar = null;
    switch (this.state.image) {
        case 'image1':
            imgVar = <NewComponent />;
            break;
        case 'image2':
            imgVar = <DifferentComponent />;
            break;
    }

    return (
      <div id='bottomSection'>
          <h2>Select Desired Asset Allocation</h2>
          <div id='pieCharts'>
            <table>
              <tr>{pieHeadMap}</tr>
            </table>
              <div>
                {chartMap}
                  <div id='test'>  
                    {imgVar}
                  </div>
              </div>
            </div>
        </div>
    );
  }

You can add a dynamic "id" to your <img> tag as below. 您可以将动态的“ id”添加到<img>标记中,如下所示。 So that based on clicked image you can render a component. 这样,您就可以基于单击的图像渲染组件。

 handleClick: function() {
    //alert(event.target.id);
    if(event.target.id === "2"){
    this.setState (
    {clicked: true}
    ); }
  },
  render: function () {
    var handleFunc = this.handleClick; //because it wasn't brining this.handleClick into the render function
    var count =0;
    var chartMap = pieCharts.map(function(prop){
      count++;
      return <img onClick={handleFunc} id={count+1} src={prop} />;
    }); 
    return (
      <div id='bottomSection'>
          <h2>Select Desired Asset Allocation</h2>
          <div id='pieCharts'>
            <table>
              <tr>{pieHeadMap}</tr>
            </table>
              <div>
                {chartMap}
                  <div id='test'>  
                    {this.state.clicked ? <TestComponent /> : null}
                  </div>
              </div>
            </div>
        </div>
    );
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM