I'm working with ReactJS.
I'm trying to get a custom attribute -name- of my ReactJS component. The e.target.name returns "undefined" on my console.
I can't figure out why.
Here my snippet- you can test it on CodeSandbox. Update: the snippet have been updated with a working solution. I use React 16.4.2 + NextJS 6.1.1 at the time of writing these lines.
export default class view_recipes extends Component {
state={
displayCategoryMenu:true,
displayBox1: false,
displayBox2: false,
displayBox3: false,
displayBox4: false,
displayBox5: false,
displayBox6: false,
};
handleDisplaying = (e) => {
e.preventDefault();
console.log("EVENT: ", e.currentTarget.attributes.reactname.value )
let display = {
displayCategoryMenu:false,
displayBox1: false,
displayBox2: false,
displayBox3: false,
displayBox4: false,
displayBox5: false,
displayBox6: false,
};
console.log("display: ", display)
}
render() {
return (
<Layout>
<div className={style.page}>
<div
style={this.state.displayCategoryMenu? {} : { display: 'none' }}
className={style.category_grid} >
<div reactname="displayBox1" onClick={(e) => this.handleDisplaying(e)} >
<Populater_s_ComponentHere />
</div>
</div>
// The functionalities above determine if I display this component
<div
style={this.state.displayBox1 ? {} : { display: 'none' }}
>
<SomeComponentHere />
</div>
</div>
</Layout>
)
}
}
The solutions of Devin Fields and Hemari Davari works well on the above snippet. Sadly seems my code block with theses greats solutions. So I have refactored my sandbox to make it more near of the reality of my code. You will see the attributes return undefined.
Here the refactored Snippet: https://codesandbox.io/s/l5k5ynv9vq .
Thanks