score:1
Here is the code. You can do it without making use of ref and use the event instead
handleClickOnLink(e,data){
console.log(e.target.id,data);
}
<li>
<a id="tester" onClick={(e) =>
this.handleClickOnLink(e,"profile")}>
Profile
</a>
</li>
score:2
You can get the id through the ref
of the <a>
. You are using the legacy ref
so I'll do it with the new way.
<li>
<a
id="profile"
ref={(ref) => { this.profile = ref; }}
onClick={() => this.handleClickOnLink(this.profile && this.profile.getAttribute('id'))}
>
Profile
</a>
</li>
score:4
The problem is that arrow functions does not bind it's own this
. Instead it actually refers to the originating context. Read docs.
One possible way to achieve what you need is using the event.target
object:
handleClickOnLink(event) {
console.info('elem id:', event.target.id);
}
render() {
return (
<ul>
<li>
<a id="profile" href="#" onClick={this.handleClickOnLink}>
Profile
</a>
</li>
</ul>
);
}
The event
(SyntheticEvent
) object is passed to elements event handlers like the onClick
. Read more here, and also how to provide extra arguments to event handlers.
Note that handleClickOnLink
function needs to be properly binded to the component instance in the class constructor, or change it's definition to an arrow function.
class App extends React.Component {
constructor(props) {
super(props);
this.handleClickOnLink = this.handleClickOnLink.bind(this);
}
handleClickOnLink(e) {
console.info('elem id ->', e.target.id);
}
render() {
return (
<ul>
<li>
<a id="profile" href="#" onClick={this.handleClickOnLink}>
Profile
</a>
</li>
<li>
<a id="extra" href="#" onClick={this.handleClickOnLink}>
Extra
</a>
</li>
</ul>
);
}
}
ReactDOM.render(<App/>, document.getElementById('app'))
<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>
<div id="app"></div>
Source: stackoverflow.com
Related Query
- how to get the id of element inside render and pass it to a function
- How to Separate Jsx inside render function into a separate file in ES6? or any other solution in react to separate the Logic and presentation?
- How do I pass the text data from firestore inside the expandable row and get the document ID to be passed inside the setState?
- React : how to pass and array from inside a Function to the return (JSX)
- How to handle stale state and get the latest value of it inside a function in react?
- React, how to access the DOM element in my render function from the same component
- Reactjs: why use const {} = this.props and why put it inside the render function
- How to return an empty jsx element from the render function in react?
- How to get the ReactJS element inside componentDidMount()?
- How to get value from useState inside the function
- How to do the comparison using if inside the render function
- How to map inside map function and show the data in Reactjs
- How to get the block unique ID inside edit and save functions on registerBlockType
- How to pass event and other arguments inside an arrow function in ReactJs
- How can I pass the argument up to the onClick function when it is called by reference inside return tag in reactjs?
- How to get the current state inside socket.io on callback function
- ReactJS: How to get response array inside State(useState) and check the length of the array when rendering/binding
- How to take user input, pass it to a function as a string, and renders the function result on the screen?
- How get data from the state and edit the element in the form?
- how to compare id of html element inside of a map function with the id of an object outside of the map function
- How to pass a javascript-generated DOM element to react render function
- React Router v6 : How to render multiple component inside and outside a div with the same path
- How to get JSON data of console.log inside function and apply it?
- how to map over an Object and render the arrays inside the object javascript/reactjs?
- How to pass props and function to the react-widow list?
- How to pass an onClick event handler a function that takes a parameter and still bind the function to the component's "this" context?
- How to dynamically and conditionally render select options inside of a map function using React Hooks
- How to get the ref.current dom element when using react-select and react refs?
- How to get borders for the react-leaflet map and check markers inside the map?
- How to make an element correctly reset its state and pass props while focusing on the input
More Query from same tag
- Testing react component enclosed in withRouter (preferably using jest/enzyme)
- React Render Children as component
- error message "undefined" when using Redux?
- My async actions endlessly called in my react redux toolkit slice
- How to rotate material-ui icon
- How to add active class to clicked item in ReactJS
- Access Individual Elements in an Array from an API (react)
- Unable to render the JSON object in a list from Github issues api
- React: How to load an image by path that contains special characters?
- React redux reducer not working correctly
- How do I stop recursive renders of a AccordionBody component inside a Accordion?
- make many styled component properties dependent on prop
- Open new page on click of button in react
- AppBar disappears between React routes
- When to use Dispatcher in React Application
- How to stylize material-ui select
- How do array equals useState declarations work in Javascript?
- I can't access the second level of a json after passing the data for props, in react js
- Mapping an object of array in Reactjs
- Re-render Canvas in React Js
- Webpack Hot Module Reloader not working with React Stateless Component
- cannot GET /PATH error when building NUXT app - DOT in the query params
- How do I bind a checkbox in a REACT Form?
- complex layout using react storybook
- Components not re render on state change
- React update state variable with JSON data
- How to access enviroment variables from laravel to react
- Unterminated jsx content while using browserroutes?
- Add item to the list in React
- Getting wordpress posts with react shows special chars instead of apostrophe