React, the map-out element addition event cannot be used

Source: Internet
Author: User

In the use of react, the react map function is often used in the same way as the map in jquery, but if you add it in each map element, you will find that the added event cannot be associated.

For example, we have a lot of comments, I need to add a reply below each comment to the beginning of the input,input is hidden, when I click on the daily comments below the reply, input box display

Here, for example, I have three sets of text in the array, I render them into Li, click each li,alert () out of each Li content, according to the following method, you will find that the error, unable to display

var myTest = React.createclass ({handlechange:function (item) {Console.log (item);},     render:function () {var Showarr y = [' Hello1 ', ' Hello2 ', ' Hello3 ']; Return (<ul>{showarry.map (function (item) {return <li onclick={this.handlechange.bind (this.item)}>{item }</li>;}) } </ul>); }); React.render (<MYTEST/>, document.body);

  

The right way to do this is not to return at the time of the map. Instead, save the results of the map to a variable, for example, we can save them in an array, and then I return the array. This will achieve our results, the code is as follows:

var myTest = React.createclass ({handlechange:function (item) {Console.log (item);},     render:function () {var Showarr y = [' Hello1 ', ' Hello2 ', ' Hello3 ']; var newarry=[];for (var i=0;i<showarry.length;i++) {var item=showarry[i];return newarry.push (<li onClick={ This.handleChange.bind (This.item)}>{item}</li>)}return (<ul>{newarry} </ul>); }); React.render (<MYTEST/>, document.body);

  

React, the map-out element addition event cannot be used

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.