Answer the question
In order to leave comments, you need to log in
How to access data attributes in React?
I 'm trying to
bind
date
attributes to a
custom
component, throwing them inside this component how to bind your attributes to the component in order to reach them through event.target ?
I do this to avoid such event handlers
https://stackblitz.com/edit/react-khcwbu?file=index.js
Answer the question
In order to leave comments, you need to log in
const MyInput = props => {
const { onChange } = this.props;
const id = this.props['data-id'];
const name = this.props['data-name'];
return <input onChange={onChange} data-id={id} data-name={name} />;
};
onChange = e => {
const { id, name } = e.target.dataset;
// do something with id and name
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question