javascript - onClick not working React js -


i trying call function when user clicks div (using onclick in react). don't need pass arguments @ moment, need call function. i'm new react.js apologies in advance ignorance. thanks.

var test = react.createclass({  btntapped: function(){     console.log('tapped!'); }, render: function() {     var stationcomponents = this.props.stations.map(function(station, index) {      return <div onclick={btntapped()}><img src="img/test.png" />{station}</div>;      });     return <div>{stationcomponents}</div>;    } });  var cards = ["amazon", "aeo", "aerie", "barnes", "bloomingdales", "bbw","bestbuy", "regal", "cvs", "ebay", "gyft", "itunes", "jcp", "panera", "staples", "walmart", "target", "sephora", "walgreens", "starbucks"];  reactdom.render(<test stations={cards} />, document.getelementbyid('test-div')); 

if have babel in build system, use es6 arrow functions.

if using es6 class creating components, use method binding @ constructor level avoid @ binding every render call , provide key div inside map function. improve performance.

class test extends react.component {    constructor(props) {      super(props);      this.btntapped = this.btntapped.bind(this);    }    btntapped() {      console.log('tapped');    }    render() {        return (        <div>          {            this.props.stations.map((station, index) => {          return <div key={index} onclick={this.btntapped}>{station}</div>        })          }          </div>        )    }  }    var cards = ["amazon", "aeo", "aerie", "barnes", "bloomingdales", "bbw", "bestbuy", "regal", "cvs", "ebay", "gyft", "itunes", "jcp", "panera", "staples", "walmart", "target", "sephora", "walgreens", "starbucks"];    reactdom.render(<test stations={cards} />, document.getelementbyid('test-div'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>  <body>    <div id="test-div"></div>  </body>


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -