Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

anchor tag (a tag) onclick event handler not working

Though i have included the onclick handler for the a tag in the html returned by the render method of the reactjs component(component with the name renderLocationLink) , though the rendering takes place correctly the onclick handler attribute doesnt appear in the rendered html on the webpage .I want the Not able to figure whats the issue , here is the code

var feedApp = React.createClass({
      getInitialState: function(){
        return {
          data : [
           {display_name:"Rao",content:"this is Rao post",links:['link1','link2','link3']},
           {display_name:"Sultan",content:"this is Sultans",links:['link4','link5','link6']},
           {display_name:"John",content:"this is John post",links:['link7','link8','link9']}
          ]
        }
      },
      fetchFeedsFromUrl: function(){
        console.log('Onclick triggered');
      },
      render: function(){
        return (<Feeds data={this.state.data} onClick={this.fetchFeedsFromUrl} />)
      }
})

var Feeds = React.createClass({

    render: function(){
      var onClickfunc = this.props.onClick; 
      var feeds = this.props.data.map(function(feed){
        return (
          <oneFeed  name={feed.display_name}  onClick={this.onClickfunc} content={feed.content}  links={feed.links} />
        )
      });
    return( 
      <div> {feeds} </div>  
    )
  }
})


var oneFeed = React.createClass({
      render: function() {
        return (
         <div> 
          <h3>{this.props.name}</h3>
          <renderLocationLink  onClick={this.props.onClick}  linkArray={this.props.links}  />
          <p>{this.props.content} </p>
         </div> 
        )
      }
    });


var renderLocationLink = React.createClass({

  render: function(){
    var onClick = this.props.onClick;
    var locationLinks = this.props.linkArray.map(function(link,index){
        return (<a onClick={this.onClick} href={link}>{link}   </a>)
    })

   return ( <div >{locationLinks}</div> )

  }
})

React.renderComponent(feedApp(null),document.body); 
like image 701
Karthic Rao Avatar asked Oct 27 '14 12:10

Karthic Rao


People also ask

Can we use onclick event on anchor tag?

It is safe to click on that link with # href; the page does leave/reload url. Follow the above advice with caution, as HTML5 rules explicitly state that href="#" is supposed to navigate to the top of the page. You can simply add the href attibute without content, and get the click behaviour.

Is Onclick an event handler?

The React onClick event handler enables you to call a function and trigger an action when a user clicks an element, such as a button, in your app. Event names are written in camelCase, so the onclick event is written as onClick in a React app.

How do I enable anchor tag?

This makes the HTML Anchor Link (HyperLink) disabled i.e. non-clickable. In order to enable a HTML Anchor Link (HyperLink), the value of its REL attribute is copied back to the HREF attribute and the REL attribute is removed. This makes the HTML Anchor Link (HyperLink) once again enabled i.e. clickable.

What is anchor (< A >) tag?

An anchor is a piece of text which marks the beginning and/or the end of a hypertext link. The text between the opening tag and the closing tag is either the start or destination (or both) of a link. Attributes of the anchor tag are as follows. HREF.


2 Answers

Your rendered markup will not contain an onClick attribute. What you write in your JSX markup is not a direct HTML markup.

What will happen instead is that React will give your markup a data-reactid attribute, and will make sure its own event handlers fire something when a specific data-reactid gets clicked.

like image 119
Daniel Apt Avatar answered Nov 13 '22 04:11

Daniel Apt


You do not need to reference "this" in your map functions to access your local variable. Remove "this" when you try to access the onClick variable.

var renderLocationLink = React.createClass({

  render: function(){
    var onClick = this.props.onClick;
    var locationLinks = this.props.linkArray.map(function(link,index){
        return (<a onClick={onClick} href={link}>{link}   </a>)
    })

   return ( <div >{locationLinks}</div> )

  }
})
like image 21
Butters Avatar answered Nov 13 '22 04:11

Butters